floating Pt. Division errors in Windows 2000

Laurent Pointal laurent.pointal at laposte.net
Mon Dec 3 21:09:56 CET 2001


[posted and mailed]

"Leon Webster" <leon at leonwebster.com> wrote in
news:05DN7.34897$H7.4336901 at ruti.visi.com: 

> I am just learning Python using the book Learning Python.  Here's a
> sample problem from the book:
>>>>a = 3
>>>>b =4
>>>>b/(2.0 + a)
> 
> When I execute this on my laptop (win2k machine, pentium III), I get
> the following answer:
> 
> 0.80000000000000004
> 
> The same program on Linux provides the right answer (0.8), as does a
> program written in Java.
>
> Does anyone know why this happens?  Is this a problem with Python on
> Windows or Win 2k?

Python display maximum number precision, this is why you see the 4e-17 
value, because its present in memory to code 0.8 in binary format.

If you request Java or C to give 20 decimal after dot, you should get same 
result. I just built this small C code:

    	int main(int argc, char* argv[])
    	{
    		printf("%0.20f",0.8);
    		return 0;
    	}

And it display (PIII+Win2K too):
    	0.80000000000000004000

Note that old Python versions act like C and Java by limitating the default 
displayed precision.

> Thanks much
> 
> Leon

A+

Laurent.




More information about the Python-list mailing list