[Tutor] help: funny numbers
Kirby Urner
urnerk@qwest.net
Tue, 2 Apr 2002 23:08:51 -0500
> So is there a function, method, or technique that can "clean up" the
> noise, and approximate the value that we really want? Or do we code it
> ourselves?
>
>
> Erik
Many techniques possible to get around floating point noise.
One is to code a rational number type (which some languages
include natively), which lets you work in entirely in integers
(numerator and denominator remain separate). Since Python
has powerful integer capabilities, you can actually get quite
far with this.
You can also find modules that do extended precision decimals.
However, the floating point values we talked about already
*do* approximate the value you really want. The difference
is miniscule. So for many if not all purposes, it suffices to
do the actual computations in floating point, and then
round/format the results to conform to two decimal digits
or whatever it is that you want to see:
>>> a = 62.3
>>> a
62.299999999999997
>>> print '%f' % a
62.300000
>>> print '%d' % a
62
Kirby