필터 지우기
필터 지우기

Problem adding the large number with small number

조회 수: 6 (최근 30일)
Erkan
Erkan 2022년 1월 8일
댓글: Star Strider 2022년 1월 9일
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.
  댓글 수: 1
Erkan
Erkan 2022년 1월 8일
How can i solve this issue, thanks in advance

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2022년 1월 8일
The addition is made. It is necessary to display the appropriate precision to see it —
a=6.3276e+16
a = 6.3276e+16
b=17.3304
b = 17.3304
a_plus_b = a + b
a_plus_b = 6.3276e+16
fprintf('%33.25E',a)
6.3276000000000000000000000E+16
fprintf('%33.25E',a_plus_b)
6.3276000000000016000000000E+16
fprintf('%33.25E',a_plus_b - a)
1.6000000000000000000000000E+01
Floating-point numbers aren’t as accurate as necessary in that respect, however thye do their best!
.
  댓글 수: 5
Erkan
Erkan 2022년 1월 9일
Thanks for answer
Star Strider
Star Strider 2022년 1월 9일
As always, my pleasure!
Espanding on this with the Symbolic Math Toolbox —
a = vpa(sym(6.3276e+16))
a = 
63276000000000000.0
b = vpa(sym(17.3304))
b = 
17.3304
a_plus_b = vpa(a + b, 30)
a_plus_b = 
63276000000000017.3304
So to retain full precision in interim calculations, use symbolic variables.
format long
a_plus_b = double(a_plus_b)
a_plus_b =
6.327600000000002e+16
fprintf('%33.25E', a_plus_b)
6.3276000000000016000000000E+16
The double conversion reverts to double-precision representation. Note that the representation is not the exact value, as computed using symbolic variables.
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by