123.4+2=125.40000000000001

Michael Hudson mwh21 at cam.ac.uk
Wed Jun 21 09:40:27 EDT 2000


Peter Koppatz <pkop at brb.midat.de> writes:

> >>> 123.4+2
> 125.40000000000001   # is this OK?
> >>> print 123.4+2
> 125.4
> >>>
> 
> On my calculator: 123.4+2 = 125.4 !?
> I am using Python 1.6a2

FWIW, on my Python:

>>> 123.4
123.40000000000001
>>> 0.4
0.40000000000000002

This isn't surprising, really, given that floats are represented in
base 2; floats are "really" rationals of the form M*(2**N) where M and
N are (bounded) integers, and 0.4 is 0.011001100110011001100110... in
binary.

Cheers,
M.

-- 
  There are 'infinite' number of developed artifacts and one cannot
  develop appreciation  for them all.  It would be all right to not
  understand something, but it would be imbecilic to put judgements
  on things one don't understand.               -- Xah, comp.lang.lisp



More information about the Python-list mailing list