Why do inconsistencies exist with floating-point numbers rounding in sprintf, fprintf, sin etc?
2 views (last 30 days)
Show older comments
MathWorks Support Team
on 31 Dec 2015
Edited: MathWorks Support Team
on 19 Apr 2021
Why do inconsistencies exist with floating-point numbers rounding in sprintf, fprintf, sin etc?
str1 = sprintf('%0.1f ',40.05);
str2 = sprintf('%0.1f ',20.05);
str3 = sprintf('%0.1f ',30.05);
str4 = sprintf('%0.1f ',50.05);
disp([str1 ' ' str2 ' ' str3 ' ' str4]);
The output is:
40.0 20.1 30.1 50.0
Accepted Answer
MathWorks Support Team
on 13 Jan 2016
It is a floating-point representation issue. The Symbolic Toolbox functions sym (with the ‘f’) option and vpa are useful for understanding why.
First, use sym with ‘f’ to determine exactly what something like 20.05 or 40.05 becomes when you convert it to double-precision floating point.
>> sym(20.05,'f')
ans =
5643573283048653/281474976710656
Now use vpa to convert that rational number to a decimal fraction with more digits:
>> vpa(ans,30)
ans =
20.0500000000000007105427357601
Repeat those steps with 40.05:
>> sym(40.05,'f')
ans =
2818268204315443/70368744177664
>> vpa(ans,30)
ans =
40.0499999999999971578290569596
You can see that 20.05 gets converted to a floating-point number slightly greater than 20.05, whereas 40.05 gets converted to a floating-point number slightly less than 40.05. The issue is not with any specific function such as sprintf, fprintf, sin etc, in fact it is not a MATLAB-related issue. It stems from the way that the IEEE-754 standard represents floating point numbers (or real numbers) in binary.
2 Comments
Stephen23
on 1 Jan 2016
Edited: MathWorks Support Team
on 19 Apr 2021
Nice introduction to the topic of floating point numbers. Any answer to this?:
Namita Vishnubhotla
on 22 Apr 2016
Please create a service request with MathWorks technical support for this question. (<mailto:support@mathworks.com support@mathworks.com>)
More Answers (0)
See Also
Categories
Find more on Number Theory 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!