1

I have a simple code:

delx=0.1;
delt = 10**-3;
r = delt/delx**2

I get r = 0.09999999999999998 in console. Why are there round-off errors for such a small number of decimal points? How do I correct this?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
rims
  • 133
  • 1
  • 8
  • You might want to read, [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – lurker Nov 25 '18 at 02:49
  • 0.1 has a small number of decimal places, but is an infinitely recurring binary fraction, so it cannot be exactly represented in binary floating point. – Patricia Shanahan Nov 25 '18 at 02:58

1 Answers1

0

Your question “Why are there round-off errors for such a small number of decimal points?” suggests you think floating-point arithmetic ought to be exact for a small number of decimal figures. However, your floating-point does not use decimal. It uses binary. (Python does not mandate this. Each Python implementation may use its own floating-point format. Most often, a format of the underlying system is used, and, most often, that is binary-based.)

Consider representing 1/3 in decimal. You could write it as .3333 or use more digits and write it as .33333333 or .333333333333. It is a “small” number—just one digit in base three—but no number of decimal digits can represent it exactly. Similarly, no number of digits in a binary base can represent .1 exactly. So you see rounding errors.

Usually, you should not seek to correct small floating-point errors. The majority of floating-point use is to approximate real arithmetic, for use where small errors are fine, such as approximating physics. We do seek to avoid large errors, and analyzing and dealing with the problems of large errors is part of a complicated subject called numerical analysis.

Floating-point is used less often for exact mathematics, although there are some applications for that. If you want exact mathematics, you should explain more of your goals and requirements than are shown in your question.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312