views:

73

answers:

2

Okay, from what I understand, an integer that is a fraction will be rounded one way or the other so that if a formula comes up with say 5/6 - it will automatically round it to 1. I have a calculation:

xyz = ((1300 - [abc intValue])/6) + 100;

xyz is defined as an NSInteger, abc is an NSString that is chosen via a UIPicker. I want the calculation (1300 - [abc intValue]) to add 1 to 100 for each 6 units below 1300. For example, 1255 should result in xyz having a value of 100 and 1254 should result in a value of 101.

Now, I understand that my formula above is wrong because of the rounding principles, but I am getting some CRAZY results from the program itself. When I punched in 1259 - I got 106. When I punched in 1255 - I got 107. Why would it behave that way?

+4  A: 

Your understanding is wrong. Integer division truncates:

5 / 6 == 0

(1300 - 1259) / 6 == 41 / 6 == 6

(1300 - 1255) / 6 == 45 / 6 = 7

You can use:

xyz = ((1300.0 - [abc intValue])/6) + 100;

and make xyz a NSDouble. That will ensure it does floating-point division.

Matthew Flaschen
Perhaps you meant NSNumber rather than NSDouble (which is not a valid type if I recall correctly)
nicktmro
A: 

You may also be confusing numbers and time. 1255 is 45 below 1300, not 5 below :-)

chrisbtoo
LOL - perhaps I should give a 2nd grade math book a try :) Thanks.
Rob