views:

93

answers:

1

I'm doing the following in Objective-C and expecting 180 as the output but I'm getting 150. Can anyone explain what I'm doing wrong?

(360 / 100) * 50

+14  A: 

You're (accidentally) using integer division. 360 / 100 is returning 3, then 3 * 50 is of course 150. To obtain a floating point result, try casting 360 or 100 to a float first, or just use a literal float - i.e., 360.0 / 100 or 360 / 100.0 or even 360.0 / 100.0.

Or, as @KennyTM pointed out in a comment, you can reorder the statement such as 360 * 50 / 100 -- this is particularly useful if a floating-point number is unacceptable for any reason.

Mark Rushakoff
Or `360 * 50 / 100`.
KennyTM
Perfect. Thanks.