The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python FAQ. Why Are Floating Point Calculations So Inaccurate in Python? How to get rid of floating point errors.
By Guest on 8th November 2022 01:32:49 AM | Syntax: PYTHON | Views: 215



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. Why are floating point calculations so inaccurate in Python?
  2. How do you stop a floating arithmetic error in Python?
  3. What is floating point error in Python?
  4. How do I get rid of floating point errors?
  5. Why am I getting strange results with simple arithmetic operations?
  6.  
  7. --
  8.  
  9. Why are floating-point calculations so inaccurate?
  10. ==================================================
  11.  
  12. Users are often surprised by results like this:
  13.  
  14.    >>> 1.2 - 1.0
  15.    0.19999999999999996
  16.  
  17. and think it is a bug in Python.  It's not.  This has little to do
  18. with Python, and much more to do with how the underlying platform
  19. handles floating-point numbers.
  20.  
  21. The "float" type in CPython uses a C "double" for storage.  A "float"
  22. object's value is stored in binary floating-point with a fixed
  23. precision (typically 53 bits) and Python uses C operations, which in
  24. turn rely on the hardware implementation in the processor, to perform
  25. floating-point operations. This means that as far as floating-point
  26. operations are concerned, Python behaves like many popular languages
  27. including C and Java.
  28.  
  29. Many numbers that can be written easily in decimal notation cannot be
  30. expressed exactly in binary floating-point.  For example, after:
  31.  
  32.    >>> x = 1.2
  33.  
  34. the value stored for "x" is a (very good) approximation to the decimal
  35. value "1.2", but is not exactly equal to it.  On a typical machine,
  36. the actual stored value is:
  37.  
  38.    1.0011001100110011001100110011001100110011001100110011 (binary)
  39.  
  40. which is exactly:
  41.  
  42.    1.1999999999999999555910790149937383830547332763671875 (decimal)
  43.  
  44. The typical precision of 53 bits provides Python floats with 15--16
  45. decimal digits of accuracy.
  46.  
  47. For a fuller explanation, please see the floating point arithmetic
  48. chapter in the Python tutorial.
  49.  
  50. Are Python floats are broken? 3 solutions in python for beginner
  51. https://www.youtube.com/watch?v=9-Cpi3hGjrY
  52.  
  53. Why Are Floating Point Calculations So Inaccurate in Python?
  54. https://www.youtube.com/watch?v=oNhSbvG24jQ
  55.  
  56. [inline]
















Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.

Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.

See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license
  • Recent Pastes