3.2*2 is 9.6 ... or maybe it isn't?

Paul Rudin paul.nospam at rudin.co.uk
Thu Jun 25 14:32:12 EDT 2009


Bojan Sudarevic <bojan at sudarevic.com> writes:

> Hi,
>
> I'm PHP developer and entirely new to Python. I installed it (version 
> 2.5.2, from Debian repos) today on the persuasion of a friend, who is a 
> Python addict.
>
> The first thing I typed into it was 3.2*3 (don't ask why I typed *that*, 
> I don*t know, I just did). And the answer wasn't 9.6.
>
> Here it is:
>
>>>> 3.2*3
> 9.6000000000000014
>
> So I became curious...
>
>>>> 3.21*3
> 9.629999999999999
>>>> (3.2*3)*2
> 19.200000000000003
> ... and so on ...
>
> After that I tried Windows version (3.1rc2), and...
>
>>>> 3.2*3
> 9.600000000000001
>
> I wasn't particularly good in math in school and university, but I'm 
> pretty sure that 3.2*3 is 9.6.

This is almost certainly nothing to do with python per se, but the
floating point implementation of your hardware.  Floating point
arithmetic on computers is not accurate to arbitrary precision. If you
want such precision use a library that supports it or make you own
translations to and from appropriate integer sums (but it's going to be
slower).



More information about the Python-list mailing list