Numerical precision in subtracting two almost identical variables?

조회 수: 6 (최근 30일)
Marco
Marco 2024년 2월 28일
편집: Stephen23 2024년 2월 28일
As far as I understand it, Matlab uses 16 digits of precision. However, I have a program that subtracts two variables and very often gives results much smaller than 1e-16. For example. When asked it displays a=0.000000006129562, b=0.000000006129562 but a-b=1.0e-24 * 0.827180612553028 How does it distinguish two variables that differ by a quantity much smaller than the 15th decimal place, if it doesn't have an internal representation of it no larger than 16 digits? Shouldn't be it a-b=0 always?
  댓글 수: 1
Stephen23
Stephen23 2024년 2월 28일
편집: Stephen23 2024년 2월 28일
In your question you incorrectly conflate two related but different topics. Here, I highlighted them for you:
"How does it distinguish two variables that differ by a quantity much smaller than the 15th decimal place, if it doesn't have an internal representation of it no larger than 16 digits?"
Decimal places is not the same thing as significant digits. Mixing up those two different things (like you are doing) is not going to help you do any computational mathematics, let alone do any science. There are many many many tutorials online which explain the difference, so I will not waste my time duplicating tutorial content here.
"Shouldn't be it a-b=0 always?"
No.

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

답변 (1개)

Aquatris
Aquatris 2024년 2월 28일
Here is your answer link
Its all in the low level things. If you display your values with 'format hex', you will see they differ by 1 bit, and that bits gives you the 1e-16 difference.
Example:
x = 0.6;
y = 6*0.1;
delta = x-y
delta = -1.1102e-16
format long
[x y]
ans = 1×2
0.600000000000000 0.600000000000000
format hex
[x y]
ans = 1×2
3fe3333333333333 3fe3333333333334
  댓글 수: 6
Marco
Marco 2024년 2월 28일
Ok, so if I understand it, the FP internal representation does not include the leading zeros. This makes sense. Is there a way to visualize the value of the two variables which difference is delta to more than 15 decimal places? Format long g does not.
For example:
Displayed are a=0.000000006129562 and b=0.000000006129562, but delta=a-b=1.0e-24 * 0.827180612553028
Then I expect it to be internally a=6.129562xxxxxxxxxe-9 and b= a=6.129562yyyyyyyyye-9
Could those decimal places in x and y made be visible?
Stephen23
Stephen23 2024년 2월 28일
편집: Stephen23 2024년 2월 28일
Use SPRINTF (results depend on the OS / MATLAB version)
Note that digits after the 16-17th have no significance.

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by