bug in multiplication operations
Show older comments
this code returns wrong result
disp(string(1899243482440473*5+1))
Why? It's a big problem. Correct result is 9496217412202366
Accepted Answer
More Answers (1)
Vilém Frynta
on 1 May 2023
Edited: Vilém Frynta
on 1 May 2023
There's probably a small error due to number being represented by bits. Number is too long and detailed, and Matlab is having a problem to accurately describe it.
Use int64.
disp(string(int64(1899243482440473)*5+1))
7 Comments
Taras
on 1 May 2023
Other thing you can do is use symbolic toolbox or use File Exchange functions.
syms x
y = x*5 + 1;
result = vpa(subs(y, x, 1899243482440473));
disp(result);
Taras
on 1 May 2023
Vilém Frynta
on 1 May 2023
yes, it's only a workaround. i'm afraid i cannot help futher, i'll leave the space to someone else who could assist you better. perhaps a Mathworks employee will appear.
if you found my answer atleast a bit helpful, i'd be happy if you could accept it.
What you want is not possible with the native datatype of Matlab (the IEEE-defined double precision floating point value):
flintmax<1899243482440473
The number you want to use is larger than the largest number where x+1 can be represented. If I give you 5 characters, the largest number you can uniquely represent is 99999. If I tell you to store 158021, you will have to round to 1.6e4.
Walter Roberson
on 1 May 2023
"Are you going to fix this problem?"
Frankly:
No, Mathworks is not going to fix this problem. This problem is inherent in any fixed-width floating point number system, no matter what the number base (base 2, base 10, base 60...). If Mathworks were to switch to 96 bit or 128 bit floating point numbers, the same problem would still exist, just at different ranges.
To "fix" the problem, Mathworks would have to switch to variable-precision numbers. There has not been hardware support for variable precision numbers since roughly the days of the IBM 1610, so calculations would all have to be done in software... pretty much the Symbolic Toolbox. Much much slower.
Walter Roberson
on 1 May 2023
There are other inherent consequences to fixed-width mathematics that cannot be "fixed" except by not using fixed-width mathematics.
Suppose you are working in 5 digit decimal, and you calculate 1/3 = 0.33333 . Add three of those together and you get 0.99999 . But 3 * (1/3) = 1 not 0.99999 . Should 5-digit decimal have represented 1/3 as 0.33334 ? Add three of those together and you get 1.00002 .
Would working in more decimal digits solve the problem? Suppose you work in 20 decimal digits, then 1/3 -> 0.33333333333333333333 and add three of those you get 0.9999999999999999999999999 not 1 exactly. If you worked with 1000 decimal digits then adding three of the representation of 1/3 would get you 1000 nines, not 1 exactly. Decimal has no finite representation of 1/3 . Switching to binary does not solve the problem: binary has no finite representation of 1/3 either. If you were to switch to base 60, then yes base 60 has an exact representation of 1/3 (as 20 / 60)... but finite base 60 representation cannot exactly represent 1/7, 1/11, 1/13, 1/17...
No matter what integer numeric base you choose, base N, base N has no finite representation for the number 1/(N+1)
The "bug" is in finite mathematics. And the only two ways to fix it are:
- Change the universe so that mathematics works differently; or
- Do not use finite mathematics.
I came across a paper a number of years ago that showed that there is a family of functions that if you calculate to any specific finite precision, that the error in the answer relative to the actual answer can be arbitrarily large. And that is a problem that affects variable-precision mathematics, where the user is permitted to specify the precision of calculations. Besides that being slow, it can give arbitrarily bad results (relative to the actual results, not in an absolute sense.) So variable-precision calculations will have "bugs" too -- bugs that cannot be fixed in any possible implementation.
Categories
Find more on Conversion Between Symbolic and Numeric in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!