Number to String Conversion Inaccuracy
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
Respuestas (1)
Jan
el 25 de En. de 2021
Editada: Jan
el 25 de En. de 2021
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.
0 comentarios
Ver también
Categorías
Más información sobre Operators and Elementary Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!