-1

when summing up values in a simple for loop, Python gives a numeric error. Any ideas what causes this behaviour?

x = 0.0
for _ in range(1000):
    x += 0.1
print(x)
>>> 99.9999999999986
stn53
  • 97
  • 6
  • Hi, thanks for the response! Yes, it does. Is there any way to circumvent this issue in Python? – stn53 Feb 07 '22 at 17:33
  • Imagine you are a computer, and you try to add 3 times the value 1/3. But, since you are a computer, you cannot know all numbers of 1/3 and approximate it by 0.33. The end result you get is 0.99 and not 1. This is the same here. A PC cannot represent 1/10 accurately in binary format, so there are some roundoff errors. There is more to that then just this, but it gives the gist. – kvantour Feb 07 '22 at 17:35
  • I would imagine the only thing that might work is a symbolic calculation with a proper python package, but I doubt that this is what you want to hear. So no, there is no way around this from a floating point perspective. – kvantour Feb 07 '22 at 17:37
  • Okay, got it. Thanks, a lot! – stn53 Feb 07 '22 at 17:37
  • You could use multi-precision floats, such as https://github.com/aleaxit/gmpy – tierriminator Feb 07 '22 at 17:40
  • There are ways to reduce the error you get (see [this](https://stackoverflow.com/q/19130042/8344060)), but this is just reducing it, and not removing it. – kvantour Feb 07 '22 at 17:41

2 Answers2

0

This is just an issue with floating point numbers in general as there are certain decimal numbers that cannot be accurately represented in the float variable type. For example in JavaScript 0.1 + 0.2 = 0.30000000000000004. Python will yield a similar result as will Java with Double types.

Do note that the result is extremely close and therefore "close enough" for financial applications and almost every other application type.

  • I was under the impression that financial applications avoided binary computations and were aiming at the usage of integer arithmetic where a unit represents something like 0.0001 currency units. Or even attempt to use floating point representations with radix 10. – kvantour Feb 07 '22 at 17:44
  • That may be the case with some intuitions, but this impossible when working with interest rates or percentages as further precision is needed. For example: Visa charges customers 2% of the total charge to process the transaction forcing the entire operation to be completed in floating point numbers. – Tyler Robbins Feb 08 '22 at 03:44
0

Floating point errors are something you will see happen in any programming language. As kvantour said, computers cannot represent fractions so if you are adding up thirds then the computer will approximate 1/3 as 0.33 and (1/3 + 1/3 + 1/3) will turn out to be 0.99 instead of the 1 it should be.

To prevent this, in most cases, you can probably round off your final answer using the python round() method and you'll get the 100 you're looking for. If you need to be more precise and rounding is not an option for you then you can use the python decimal module as such:

from decimal import Decimal as D
x = D(0)
for _ in range(1000):
    x += D("0.1")
print(x)
ZeroDayTea
  • 16
  • 4