Are floats inaccurate?

One often thinks that the use of decimals provides the most accurate recording of numbers with decimal places.

Whilst that is the case when you don't have any rounding. At the point that you round data you are starting to loose accuracy.

Colin has blogged about the use of floats. "Do you use the float datatype?". Which has got a good discussion started on it.

If you are performing multiple calculations where you are multiplying numbers with decimal places then you can soon end up requiring lots of decimal places to maintain total accuracy. However the second you start dividing numbers you are in a whole world of in accuracy.

So you need split a payment of £10 3 ways. Who gets what, 3.33 to each and then 1 p remaining.



-
Published 30 October 2007 23:13 by simonsabin

Comments

05 November 2007 11:24 by DamianMulvena

# re: Are floats inaccurate?

That 1p remainder is precisely why you should use decimal types. If you use float you still can't give the final 1p to anyone, but at least with decimal types you aren't fooled into thinking that you can.

You have to come up with a means of dealing with the remainder rather than chopping it up endlessly.