Q:

Objective C - iOS - Strange fmod() behaviour

Can anybody help me explain the following? You can type the same numbers into Google and get the same results as I am getting in code....
1089.80 % 0.04 = 0
1089.84 % 0.04 = 0.04 - Surely this is wrong? 0.04 goes into 1089.84 with no remainder.
1089.88 % 0.04 = 0
1089.92 % 0.04 = 0

Similar behaviour appears for other numbers that should seemingly be correct.
Thanks in advance!

A:

You're looking at the rounded values.  Internally the number is not 1089.84 but something like 1089.84000026.
If you want "correct" values, multiply both sides by 100, add 0.5, and (int) them, then take the integer mod, and convert the result back to float.

A:

Indeed, 0.04 goes into 1089.84 with no remainder.  However, that is not the computation that you are asking the computer to perform for you.
Neither 1089.84 nor 0.04 is representable as a floating point number.  Thus, each of the double-precision literals is rounded to the closest representable value.  What you are actually computing is:
1089.839999999999918145476840436458587646484375 % 0.040000000000000000832667268468867405317723751068115234375

which is precisely
0.039999999999895459457111002166129765100777149200439453125

when you print the result, it is being rounded to a few decimal places for display, and so you see 0.04.

