Number to String Conversion Inaccuracy

1 回表示 (過去 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 Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by