필터 지우기
필터 지우기

Number to String Conversion Inaccuracy

조회 수: 2 (최근 30일)
Thomas Sawyer
Thomas Sawyer 2021년 1월 25일
편집: Stephen23 2021년 1월 25일
I have a case where I have a whole number that is 19 digits long (1000000000000000001). If you pass this into num2str or sprintf the results differ from the original number. Now I know the result is a string and the data types are different but their representations should be identical. Below is an example.
A = 1000000000000000001;
sprintf('%0.20g', A);
Results:
ans =
'1000000000000000000'
If you change A to equal 10000000000000000002 the results are still the same. These aren't the only values that are subjected to this problem. Every number I have tried with 19 digits or more has this problem.
  댓글 수: 2
Ive J
Ive J 2021년 1월 25일
편집: Ive J 2021년 1월 25일
I don't think it's an issue with num2str itself. Even if you compare A to 1e18, they're equal for MATLAB
A = 1000000000000000001;
A == 1e18
ans =
logical
1
VPI Toolbox may be of help.

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

답변 (1개)

Jan
Jan 2021년 1월 25일
편집: Jan 2021년 1월 25일
Matlab uses the default type double, which is defined by the IEEE 754 standard. This type is implemented in the CPU hardware also. Doubles store about 16 decimal digits and an exponent in 64 bits.
This means, that 19 decimal digits cannot be stored accurately in a double precision variable. It is not a problem of the conversion to a string, but
A = 1000000000000000001;
crops the rightmost digits already.
Another example, where this matters:
1e17 + 1 - 1e17 % 0
This replies 0 instead of the mathematical result 1. Reordering the terms produces a different result:
1e17 - 1e17 + 1 % 1
These effects have to be considered for computations with limited precision. The corresponding techniques belong to the field of numerical maths.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by