arthur
arthur

Reputation: 348

How come 0.7 - 0.5 - 0.2 => -5.551115123125783e-17?

I have no idea how to deal with problems like this:

0.7. - 0.5. - 0.2 # => -5.551115123125783e-17

An article links to What Every Computer Scientist Should Know About Floating-Point Arithmetic and THE FLOATING-POINT GUIDE. They describe why some approximation, and therefore inaccuracy, is unavoidable. But I still have no idea. Any explanation of why the result is remote from expected as well as why a calculator does well with a problem would be helpful for understanding of the problem.

Upvotes: 0

Views: 1687

Answers (2)

Will
Will

Reputation: 4713

As you have worked out from the resources you link to floating point numbers are an approximation. You are right that with floating point numbers you are going to have inaccuracies representation.

In most cases these small inaccuracies end up mostly cancelling out when performing calculations. E.g. something just larger than 5 + something just smaller than 3 is something almost exactly 8.

The accuracy that floating point can represent values varies depending on the values themselves. Numbers that are between -1 and 0 and 0 and 1 are more difficult to represent accurately the closer they are to 0. Numbers outside this range get less accurate the further away from 0 they are.

The case you have shown is one where the inaccuracies in representing .7, .5 and .2 have all added up to an even larger inaccuracy in the result.

As for calculators, most calculators don't actually use binary floating point numbers. They use either arbitrary precision fixed point ones, or decimal floating point instead. Fixed point numbers don't have the kind of inaccuracies of precision that floating point numbers do. This comes at the expense of the range of numbers that they are capable of representing though. Decimal floating point doesn't have the same kind of error of representation, as it doesn't have to convert numbers from decimal to binary before storing them. It can still suffer from rounding errors though.

Upvotes: 1

thus spake a.k.
thus spake a.k.

Reputation: 1637

Floating point numbers are (usually) binary and some fractions can only be approximated by binary fractions with a finite number of digits, leading to the behaviour that you're seeing.
Note that exactly the same problem occurs with decimal fractions. For example (to 6 decimal places)

    1.0 / 3.0 = 0.333333
    3.0 * 0.333333 = 0.999999

I think that the reason why binary approximations of fractions can seem suprising whilst decimal ones don't is simply that we're so familiar with the latter that we barely even notice them any more!

Upvotes: 0

Related Questions