- given that one of your input values and also the expected result cannot be exactly represented by double floating point numbers, how do you expect them to be exactly represented by double floating point numbers?
- If this is not a behavior that you think is due to the nature of binary floating point numbers, then what are the obeservable behaviors of binary floating point numbers? Should they behave like the numbers you learned about at school?
Numerical error representing data in Format Long
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
SwadFX
el 27 de Mayo de 2020
Dear all,
I have had a surprise making with this simple multiplication:
>> format long
>> 10e3*820e-12
ans =
8.199999999999999e-06
The result must be 8.2e-6!!!
I have the correct result doing before "format short":
>> format short
>> 10e3*820e-12
ans =
8.2000e-06
The answers based in the binary nature of the machine and rounding, doesn't convince me in this case.
Is this a bug???
Best regards,
Javier
0 comentarios
Respuesta aceptada
Stephen23
el 27 de Mayo de 2020
Editada: Stephen23
el 27 de Mayo de 2020
"I have had a surprise making with this simple multiplication:"
Nothing in that result is surprising.
"The result must be 8.2e-6!!!"
In fact the result cannot be exactly 8.2e-6, simply because there is no such value in (double) binary floating point. The closest value is actually (here I used James Tursa's excellent FEX submission num2strexact):
>> N = 8.2e-6;
>> num2strexact(N)
ans =
8.19999999999999941717628015869223645495367236435413360595703125e-6
The closest representable value on the other side is:
>> num2hex(N)
ans =
'3ee132576b20e04a'
>> num2strexact(hex2num('3ee132576b20e04b'))
ans =
8.200000000000001111242174667292914591598673723638057708740234375e-6
So, it is clear that your expected value cannot be exactly represented using double floating point.
Lets now have a look at the input value which also cannot be exactly representated by double floating point numbers:
>> num2strexact(820e-12)
ans =
8.1999999999999996008103761454643802764064020038858870975673198699951171875e-10
and the actual product of those two values:
>> num2strexact(10e3*820e-12)
ans =
8.19999999999999941717628015869223645495367236435413360595703125e-6
So far nothing unexpected, in fact I'm impressed that the multiplication gave the nearest representable value.
"I have the correct result doing before "format short":"
It is not more nor less correct than the long format, just exactly the same value stored in memory displayed to a different number of significant digits. Changing the displayed decimal precision makes absolutely no difference the the value stored in memory, nor does it make any difference to the binary precision of that value stored in memory.
"The answers based in the binary nature of the machine and rounding, doesn't convince me in this case."
Two questions:
"Is this a bug???"
No.
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!