1

Good day,

I'm getting a strange rounding error and I'm unsure as to why.

print(-0.0075+0.005)

is coming out in the terminal as

-0.0024999999999999996

Which appears to be throwing off the math in the rest of my program. Given that the numbers in this function could be anywhere between 1 and 0.0001, the number of decimal places can vary.

Any ideas why I'm not getting the expected answer of -0.0025

1 Answers1

2

Joe, The 'rounding error' you refer to is a consequence of the arithmetic system Python is using to perform the requested operation (though by no means limited to Python!).

All real numbers need to be represented by a finite number of bits in a computer program, thus they are 'rounded' to a suitable representation. This is necessary because with a finite number of bits it is not possible to represent all real numbers exactly (or even a finite interval of the numbers in the real line). So while you may define a variable to be 'a=0.005', the computer will store it as something very close to, but not exactly, that. Typically this rounding is done through floating-point representations which is the case in standard Python. In the binary version of this system, real numbers are approximated by integers multiplied by powers of 2 for their representation.

Consequently, operations such as the sum that you are performing operate on this 'rounded' version of the numbers and return another rounded version of the result. This implies that the arithmetic in the computer is always approximate, although usually, it is precise enough that we do not care. If these rounding errors are too large for your application, you may try to switch to use a more precise representation (more bits). You can find a good explainer with examples on Python's docs.

pabloi
  • 21
  • 3
  • Note a *finite* number of bits does not preclude an *arbitrary* number of bits. Just like Python has arbitrary precision integers, it is also possible to [represent rational numbers with arbitrary precision](https://docs.python.org/3/library/fractions.html). Any decimal number can be represented this way. Fixed-precision rationals are the default because they are significantly faster, not because there is no alternative. – MisterMiyagi Jun 17 '20 at 15:47
  • Thanks for your feedback. Am I right in understanding that the floating points used to represent the numbers will not change the decimal value of the numbers. Therefore, if I leave all the calculations as they are and convert the final outputs using the Decimal function I will have the intended representation? Or is there potential for these floating points to cause errors within the calculations? – Joseph Joe Soltan Jun 18 '20 at 11:06
  • Joe, not sure what you mean by 'will not change the decimal value of the numbers'. If you mean that when you do a= 0.05 then a will exactly be 0.05, the answer is in general no. Because of the use of binary floating point, most finite decimal expressions cannot be represented exactly. For example, 0.5 can be represented exactly (essentially as 1x2^-1) but 0.05 cannot. It may seem that decimals are usually expressed exactly, but that is only because of the rounding off that is done when displaying numbers (i.e. only the first N digits are shown). – pabloi Jun 18 '20 at 23:42