How can I avoid a small value ignored during calculation?
Show older comments
z is a small value, and wen a1 is added by z, it doesn't show any difference. Why is that? And how can I aviod this?

Accepted Answer
More Answers (1)
Patrik Forssén
on 20 Nov 2022
Edited: Patrik Forssén
on 20 Nov 2022
@John D'Errico explained why this happens. If you really need to avoid this, you must therefore use an arbitrary-precision numerical class for your calculations. MATLAB does not have one, but you can interface Java’s. Here is what your example would look like,
z1 = java.math.BigDecimal('1.111111e-19');
a1 = java.math.BigDecimal('-0.0581375401465599531136696498379023978486657142639160156250000000000000');
a2 = a1.add(z1);
disp(char(z1.toPlainString()))
disp(char(a1.toPlainString()))
disp(char(a2.toPlainString()))
Categories
Find more on Operating on Diagonal Matrices 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!