42 views (last 30 days)
Show older comments
Javad Farzi on 8 Nov 2022
Commented: Walter Roberson on 15 Nov 2022
It is obvious that
|0.1234-0.123| = 0.0004
however, the following Matlab result is a little bit different!
I think this happens because the IEEE754 conversion of 0.1234 and 0.123 have infinite digits.
Does anyone have more explanation?
Star Strider on 8 Nov 2022
Edited: Star Strider on 12 Nov 2022
‘I think this happens because the IEEE754 conversion of 0.1234 and 0.123 have infinite digits.’
‘Does anyone have more explanation?’
I doubt it. Your explanation about covers it. (The relevant MATLAB documentation is in Floating-Point Numbers.)
EDIT — (12 Nov 2022 at 11:44)
‘In double precision we can trust on about 15 decimal digits.In double precision we can trust on about 15 decimal digits.’
See the eps function for details —
FPP = eps
FPP = 1/eps
FPPlog2 = log2(eps)
FPPlog2 = log2(1/eps)
James Tursa on 8 Nov 2022
Edited: James Tursa on 8 Nov 2022
"... the IEEE754 conversion of 0.1234 and 0.123 have infinite digits ..."
Not sure what you mean by this statement. Neither of these numbers can be represented exactly in IEEE double precision floating point which of course uses a finite number of binary bits. Maybe you mean an exact conversion of these specific decimal numbers to binary would require an infinite number of binary bits?
The exact binary-to-decimal conversions of the numbers that are stored (the closest representable values to the decimal numbers above) are:
fprintf('%.56f',0.1234 - 0.123)
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!