On Nov 2, 2009, at 11:01 AM, Chunk 1978 wrote:

i just came across some code, and i'm trying to understand why the developer chose to use the pow() function instead of simply multiplying as the 2nd arguments are always 2. i'm certainly no mathematician, but if the 2nd argument is going to be 2, why use pow()? is it just a matter of taste?

why use this:

float dist = pow(red - r, 2) + pow(green - g, 2) + pow(blue - b, 2);


instead of:

float dist = ((red - r) *  2) + ((green - g) * 2) + ((blue - b) * 2);
_______________________________________________

Those two lines mean entirely different things. x * 2 != x ^ 2. Consider x = 3. 3 * 2 = 6. 3 ^ 2 = 9.

If the author didn't want to use the pow function, he/she might streamline by doing x * x, but it's possible the author just didn't want to have to write all that out, and felt that using the pow function was more immediately clear what was going on without having to write or read much.

Luke
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to