필터 지우기
필터 지우기

Operation with large and small numbers

조회 수: 8 (최근 30일)
Franklin
Franklin 2023년 1월 5일
댓글: Image Analyst 2023년 1월 6일
1^4 + 1^4 + 23512^4 - 23512^4 = 2
but
Matlab says
1^4 + 1^4 + 23512^4 - 23512^4 = 0.
  댓글 수: 1
Franklin
Franklin 2023년 1월 5일
Please what is the quickest way to solve this problem? I need help with variable precision integer arithmetic.

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

답변 (2개)

VBBV
VBBV 2023년 1월 5일
(1^4 + 1^4) + (23512^4 - 23512^4)
  댓글 수: 2
VBBV
VBBV 2023년 1월 5일
편집: VBBV 2023년 1월 5일
Add operation is done first on numbers and then subtracts. To get desired result use () which shows precedence in operator actions on numbers
Franklin
Franklin 2023년 1월 6일
Thank you.
My numbers are variable. The order in wich they are grouped in parenthesis is important. (1^4 + 23512^4) + (1^4 - 23512^4) = 0.

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


Image Analyst
Image Analyst 2023년 1월 5일
It's called truncation error. You should have learned about it in your linear algebra or numerical analysis course.
Basically it's caused by adding a gigantic number to a small number (2). There are not enough digits in 64 bits to accurately represent the 2 in the ones column of the number. The number if basically truncated and the 2 and perhaps even some other digits (like the 10's place) are lost.
To fix, use parentheses:
result = (1^4 + 1^4) + (23512^4 - 23512^4)
The first term is 2 and the second term is 0. Those numbers are in the same range and can be added with no truncation.
  댓글 수: 2
Franklin
Franklin 2023년 1월 6일
Thank you.
My numbers are variable. The order in wich they are grouped in parenthesis is important. (1^4 + 23512^4) + (1^4 - 23512^4) = 0.
Image Analyst
Image Analyst 2023년 1월 6일
See the FAQ:
You're still bound by truncation error.
Why is the order important, and why do you have such enormous numbers added to such tiny numbers?
You might have some luck with the symbolic toolbox, but I don't have that so can't advise you on that. Call tech support about that.

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

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by