Strangeness with basic math

Delaney, Timothy tdelaney at avaya.com
Tue Sep 4 04:08:42 EDT 2001


> I have been racking my brains trying to RTFM to explain the following
> 
> bash-2.03$ python
> Python 2.1 (#1, Jul  3 2001, 10:26:19) 
> [GCC 2.95.2 19991024 (release)] on sunos5
> Type "copyright", "credits" or "license" for more information.
> >>> 1923.23 + 17023.34
> 18946.57
> >>> 1923.23 - 17023.34
> -15100.110000000001
> >>> print 1923.23 - 17023.34
> -15100.11
> >>> 
> 
> Why when I do 1923.23 - 17023.34 am I getting -15100.110000000001
> on NT I get the same thing. Under Python 1.5.2 I get the same 
> and result as 
> the print 1923.23 - 17023.34 (ie -15100.11)

First thing to do is to read up on "floating point math".

Hint - print is displaying things "nicely" for you - however, the other
values you see are the real thing (or as close an approximation as
possible). Python 1.5.2 do the Wrong Thing (TM).

Tim Delaney




More information about the Python-list mailing list