Problem adding the large number with small number
6 ビュー (過去 30 日間)
古いコメントを表示
Hello everyone, I have a problem with adding numbers.
I have two numbers;
a=6.3276e+16 and b=17.3304, when adding these numbers, the result is a, that is, no addition is made.
採用された回答
Star Strider
2022 年 1 月 8 日
The addition is made. It is necessary to display the appropriate precision to see it —
a=6.3276e+16
b=17.3304
a_plus_b = a + b
fprintf('%33.25E',a)
fprintf('%33.25E',a_plus_b)
fprintf('%33.25E',a_plus_b - a)
Floating-point numbers aren’t as accurate as necessary in that respect, however thye do their best!
.
5 件のコメント
Star Strider
2022 年 1 月 9 日
As always, my pleasure!
Espanding on this with the Symbolic Math Toolbox —
a = vpa(sym(6.3276e+16))
b = vpa(sym(17.3304))
a_plus_b = vpa(a + b, 30)
So to retain full precision in interim calculations, use symbolic variables.
format long
a_plus_b = double(a_plus_b)
fprintf('%33.25E', a_plus_b)
The double conversion reverts to double-precision representation. Note that the representation is not the exact value, as computed using symbolic variables.
.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Numbers and Precision についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!