Azzera filtri
Azzera filtri

Matlab basic arithmetics error | Sum rounding

6 visualizzazioni (ultimi 30 giorni)
Marius Marinescu
Marius Marinescu il 1 Feb 2021
Modificato: Jan il 3 Feb 2021
Hello Everyone,
I have notice a curious "property" of Matlab. It is not able to compute a sum with finite decimal (two decimals). On one hand, when I compute the follwoing some in format long I get this:
>> format long
>> 2.15+1.4+0.3+1.85
ans =
Which differs from the true value:
>> 5.7 - (2.15+1.4+0.3+1.85)
ans =
>> eps
ans =
(By the way, how is possible the diference between the true value and the Matlab computation is more than the "epsilon" of the machine?)
In the other hand, if we do the same in format short we get:
>> format short
>> 2.15+1.4+0.3+1.85
ans =
>> 5.7-ans
ans =
So it show "5.7000" but it really saves 5.6999... with some high precision.
Why is this? And why the sum with only two decimals has a rounding error? I suspect is because how Matlab computes the sum in binary numbers. In some step of the computation a periodic binary number must appear and spoils the sum.

Risposte (1)

Jan il 3 Feb 2021
Modificato: Jan il 3 Feb 2021
Welcome to the world of numerical maths. As Stephen has explained already, calculations with limited precision are expected to have rounding errors. Most fractional values cannot be represented accurately as binary numbers such that even 0.1+0.1+0.1 ~= 0.3 .
Activating a rounding in the output by format short rounds the outputs.
how is possible the diference between the true value and the Matlab computation is more than the "epsilon" of the machine?
Epsilon is the smallest number which satisfies 1+eps ~= 1 with limited precision. This means, that 2+eps does equal 2. The value of the truncation error depends on the magnitude of the number. Therfore the command eps() accepts the value as input argument:
eps(1) % 1 is the default value
In consequence building the sum of 2.15, 1.4, 0.3, 1.85 can accumulate the eps values for the different operations. eps(5.7) = 8.8818e-16, so this is the expected error.
The effects of computations with limited precision are well known and not "curious". They are explained in the first two lessons of numerical maths and on the first pages of each book about this topic. Especially the cancellation is important, which occurs if you subtract two almost equal numbers, e.g. in the quotient of differences:
diff_F = (F(x + h) - F(x)) / h
If h is chosen too small, the difference in the counter is dominated by rounding errors and dividing by a tiny value let the error explode.


Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by