fprintf('One number is %f\n', 436598989898987.124132)

5 visualizaciones (últimos 30 días)
Lisa Lee
Lisa Lee el 4 de Sept. de 2017
Editada: Mark Schwab el 7 de Sept. de 2017
I am wondering if someone can explain why after I input the command below:
fprintf('One number is %f\n', 436598989898987.124132)
The result I received was:
One number is 436598989898987.120000
I am curious about the difference between the two numbers.
Thank you in advance, - Lisa
  2 comentarios
Cedric
Cedric el 4 de Sept. de 2017
Editada: Cedric el 4 de Sept. de 2017
Look at the eps documentation and then at these two cases:
fprintf('One number is %f\n', 436598989898987.124132 + eps( 436598989898987 ))
One number is 436598989898987.190000
fprintf('One number is %f\n', 436598989898987.124132 + eps( 436598989898987 )/2)
One number is 436598989898987.120000
If you want to see how the order of magnitude of the distance varies with the powers of 10
plot( 0:20, log10( eps( 10.^(0:20) ))) ;
grid ;
and you'll be able to find at 0 ( 10^0=1 ) the 2^-52 of the doc:
log10( 2^-52 )
ans =
-15.6536
Walter Roberson
Walter Roberson el 4 de Sept. de 2017
>> eps(436598989898987.124132)
ans =
0.0625
Your number is only stored internally to within the nearest 0.0625
We can tell from the output that you are using MS Windows; on OS-X or Linux you would have gotten
One number is 436598989898987.125000
The MS Windows libraries are poor at formatting values exactly.

Iniciar sesión para comentar.

Respuestas (1)

Mark Schwab
Mark Schwab el 7 de Sept. de 2017
Editada: Mark Schwab el 7 de Sept. de 2017
The default data type for storing numbers in MATLAB is double. The double data type is a 64 bit data type that stores numbers in a representation similar to scientific notation. Within the 64 bits, one is reserved to specify sign (positive or negative). The next 11 bits are reserved for the exponent power and the remaining 52 bits are used for the fractional value. The number you are trying to store would require more than 52 bits to represent the fractional portion with exact precision so MATLAB uses floating point precision to round to a number it can represent in memory.
Please refer to the following documentation for more information: https://www.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by