[Pythonmac-SIG] possible bug with division operator and
floating point numbers
John W Baxter
jwblist@olympus.net
Thu, 18 Jan 2001 14:49:14 -0800
At 17:02 -0500 1/17/01, Gordon Worley wrote:
>Since the original poster mentioned being new to Python, a bit of
>slicing will take care of display problems, though won't fix the
>number as a float:
>
>num = 4.0 / 5.0
>`num`[:-1]
>
>slices off that last 4, which is easy enough. Something is the
>matter, though, because:
>
>float(`num`[:-1])
>
>yields
>
>0 .80000000000000004
>
>and even just typing 0.8 in the interpreter returns the above number.
>There is bound to be a work around, but should be discussed
>elsewhere, since it isn't Mac specific.
Nothing is wrong, except that binary floating point is being used. 0.8 is
not exactly representable (at any number of bits) in binary floating point.
(20% of the single digit decimals are: x.0 and x.5...the others are not.)
To make things more fun, some computations which "we know" result in .8
won't result in .80000000000000004
Consider
>>> 0.3 + 0.3 + 0.3 - 0.1
0.79999999999999993
(which [I think] at least shows that the .8...04 value is closer than the
next lower possible value, a Good Thing, since that's how it's supposed to
be).
The above example along with
>>> .03
0.029999999999999999
make slicing NOT the way to do it in general.
The solution is to use the right tool. Which often isn't floating point.
(Or, for money, decimal floating point: in the early days when BASIC was
Microsoft's primary product, they shipped two versions, one using binary
floating point for speed, the other using decimal floating point for
sanity.)
--John
--
John Baxter jwblist@olympus.net Port Ludlow, WA, USA