Simple addition

Dan Bishop danb_83 at yahoo.com
Mon Feb 16 15:08:49 EST 2004


Mathieu Malaterre <mmalater at nycap.rr.com> wrote in message news:<LWPXb.58968$n62.56888 at twister.nyroc.rr.com>...
> Hi,
> 
>     I tried a simple addition with python and I don't understand what is 
> going on:
> 
> $python
>  >>464.73+279.78
> 744.50999999999999

You computer does arithmetic in binary.  None of these numbers can be
exactly represented as a binary fraction.

464.73 = bin 111010000.10 11101011100001010001 11101011100001010001...
279.78 = bin 100010111.1 10001111010111000010 10001111010111000010...

They get rounded to the nearest 53-bit float:

464.73 ~= 0x1.D0BAE147AE147p+8
279.78 ~= 0x1.17C7AE147AE14p+8
          --------------------
          0x2.E8828F5C28F5Bp+8
       ~= 0x1.744147AE147AEp+9 after normalization

The exact decimal equivalent of this sum is
744.509999999999990905052982270717620849609375.  Python's repr()
rounds this to 17 decimal digits, or "744.50999999999999".



More information about the Python-list mailing list