[Shootout-list] floating point rounding

Daniel South WildCard_25@HotMail.Com
Fri, 1 Apr 2005 03:57:39 +1000


Joel Hoffman wrote:
|
| Well, that's not really a general solution but it would solve this
| specific problem. Simply printing the count as an integer would
| resolve it entirely.

Agreed.

>snipped<|
| Try this with gcc to see what I mean. It is definitely NOT rounding
| toward even numbers, else 0.495 and 0.505 would both round to 0.5. But
| 0.495 is only approximately represented as 0.494999999999....
|
Yes, that is a common problem with fp. As the binary rep of 0.495 is 
just a shade smaller, when it rounds it does so correctly. That is it 
rounds down to 0.49 as it is smaller than 0.495. Isn't fp maths fun :-)

Daniel South