3

I encounter the following issue accidentally (shown in the screenshot) which is very odd to me. The output[4] and [5] is obviously wrong. But with the input value of 991.91, it turns out to be right (output[6]).

Does any one have an idea what might be the reason of this? Much appreciated!

I am running Mathematica 10.0.1.(student edition) on Mac OS Captain system.

enter image description here

Karsten7
  • 27,448
  • 5
  • 73
  • 134
Pünktchen
  • 31
  • 2

1 Answers1

10

This is not a bug.

It is an expected result of numerical roundoff error and the somewhat unusual way Mathematica computes division.

What is roundoff error? Floating point numbers have a finite precision. With almost any arithmetic operation performed, the result is not exact: digits beyond about the 16th get discarded.

What's special about how Mathematica computes division? a/b is really computed as a*(1/b). To prevent this one must write Divide[a,b] instead.

When you write a/a, the system computes 1/a first, but the result will not be precise to more than 16 digits ($MachinePrecision). Thus multiplying it by a does not give 1. precisely, but a result very slightly larger (in this case).


These phenomena are not unique to Mathematica. They happen whenever computing with floating point numbers on a computer (and in fact Mathematica attempts to protect against them to some extent by allowing a tolerance with equality comparisons, etc.) To fully understand what happens in different cases we also need to consider that floating point numbers are represented in binary, not decimal. It is not exactly the case that digits beyond the 16th decimal are discarded. It is more accurate (but still not the full story) to say that digits beyond the 52nd binary are discarded. The usual recommended reading about these issues is:

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • also, FWIW, in cases when you work with collections of numbers that are both reasonably large (x >> 10^10 by default) and subject to float rounding errors (e.g. Fourier transforms), you can always simply use Chop[] (https://reference.wolfram.com/language/ref/Chop.html) –  Jan 14 '16 at 13:04