0

Possible Duplicate:
Python float - str - float weirdness

Python float division does not appear to have accurate results. Can someone explain why?

>>>3.0/5
0.59999999999999998
Community
  • 1
  • 1
deadghost
  • 5,017
  • 3
  • 34
  • 46
  • 5
    must be one of the most commonly asked questions on SO in every conceivable language that supports floating point. Pity a search didn't find anything...oh wait! : http://www.google.com.au/#sclient=psy-ab&hl=en&source=hp&q=site:stackoverflow.com+python+float+exact&pbx=1&oq=site:stackoverflow.com+python+float+exact&aq=f&aqi=&aql=&gs_sm=e&gs_upl=3844l4441l1l4674l4l4l0l0l0l2l319l1070l2-3.1l4l0&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=4ea33f08981ef7d&biw=1153&bih=974 – Mitch Wheat Nov 14 '11 at 11:34
  • 1
    @MitchWheat Well, now this is the first result :) – rplnt Nov 14 '11 at 11:47

2 Answers2

3

Short answer: Floats use finite-precision binary encoding to represent numbers, so various operations lose some precision.

The Wikipedia page has a lot of information (maybe too much).

See also: How do I use accurate float arithmetic in Python?

Community
  • 1
  • 1
taleinat
  • 8,441
  • 1
  • 30
  • 44
1

Floating point arithmetic is not exact; there are rounding errors that are worsened by the fact that computers use binary floating point and not decimal floating point. See Wikipedia.

ibid
  • 3,891
  • 22
  • 17