0

How can I properly calculate with floats in Python? Whenever I calculate normally, the numbers are so inaccurate. I expected more from a numeric-oriented language like Python.

Python Version: 3.7

Code:

4.2+8.9

Result:

13.100000000000001

This might not seem like much, but since I am writing an algorithm that adds numbers to floating point numbers hundreds of times, this starts to become an issue.

Relaxisys
  • 1
  • 1
  • Can you provide an example of what you mean by 'inaccurate', together with the code and python version you used to obtain the inaccurate results? – dzang May 16 '20 at 11:23
  • @Relaxisys--Python provides several options for increased precision over normal floating-point. One option is using the [Decimal module](https://docs.python.org/2/library/decimal.html). In this case Decimal('4.2')+Decimal('8.9') equals 13.1 – DarrylG May 16 '20 at 11:49
  • Are you dealing with something discrete, like money amounts, or with the results of physical measurements? – Patricia Shanahan May 16 '20 at 15:14

0 Answers0