faqts : Computers : Programming : Languages : Python : Common Problems : Maths

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

9 of 9 people (100%) answered Yes
Recently 7 of 7 people (100%) answered Yes

Entry

Why does 123.4+2=125.40000000000001?

Jun 22nd, 2000 04:25
unknown unknown, Ruud de Rooij


From http://www.python.org/1.6/

  The repr() of floating point numbers now gives full (17-digit)
  precision. This means that a number like 8.1 (which cannot be
  represented exactly in binary) will show up as 8.0999999999999996 when
  displayed using repr(). This shouldn't break any code but it may
  confuse users, especially since the interactive shell uses repr() to
  display all floating point results. Use str() or explicit precision in
  string format (e.g. "%.12g" % x) to see the the more familiar, rounded
  form.