I develop a c++ app (Windows 7, 64 Bit, VS 2008) where the following formula is used (all vars are of type double):
mValue = floor(mValue/mStepping)*mStepping;
The idea is to shorten a number to a given count of decimal places. I think there are better ways to do this, but this is not the question here (but if you have a better option, please bring it on!).
mValue comes from user input, so in most cases the number of decimal places is already ok. But for some cases the output differs from the input.
For example mStepping has a value of 0.1 (which should round to one decimal place). Now if mValue has a value of 14.6, everything is ok. If mValue is 14.7 the result is 14.6.
So, why is floor(14.7/0.1)*0.1 = 14.6 ?
I tested other values and around 20% of them differed by 0.1. I digged further and discovered, that 14.7/0.1 has a different binary encoding than 147.0:
14.7/0.1 = ff ff ff ff ff 5f 62 40
147.0 = 00 00 00 00 00 60 62 40
I understand that the same number can be encoded differently as a double. But why does floor() handle them differently? And what can I do against it?