Question about floating point

Gregory Ewing greg.ewing at canterbury.ac.nz
Tue Aug 28 19:31:29 EDT 2018


Frank Millman wrote:
> I have been trying to explain why 
> they should use the decimal module. They have had a counter-argument 
> from someone else who says they should just use the rounding technique 
> in my third example above.

It's possible to get away with this by judicious use of rounding.
There's a business software package I work with that does this --
every number is rounded to a defined number of decimal places
before being stored in its database, so the small inaccuracies
resulting from inexact representations don't get a chance to
accumulate.

If you're going to do this, I would NOT recommend using the
rounding technique in your example -- it seems to itself be
relying on accidents of the arithmetic. Use the round()
function:

 >>> 1.1 + 2.2
3.3000000000000003
 >>> round(1.1 + 2.2, 1)
3.3

Also, if you go this route, always keep in mind *why* it works --
it relies on the actual value always being close enough to
a multiple of a power of 10 that it rounds to the correct
value when converted to decimal.

> Are there edge cases where that rounding method could fail?

Yes, it's easy to come up with examples that make it fail,
e.g. multiplying by a large number, or adding up a few billion
monetary amounts before rounding. These things are very
unlikely to happen in an accounting application, but they
are theoretically possible.

So, you can probably make it work if you're careful, but for
peace of mind I would still recommend using Decimal, because
Python has it, and it eliminates all of these potential
problems.

-- 
Greg



More information about the Python-list mailing list