Format longeng - rounding error

3 次查看(过去 30 天)
Hello All,
When using "format longeng", I am getting strange results to basic maths but when using "format short" the correct / expected answer is returned:
>> format short
>> 0.088 - 0.08
ans =
0.0080
>> format longeng
>> 0.088 - 0.08
ans =
7.99999999999999e-003
Is this a bug? Am I missing something? would this make a difference to my simulations?
For reference, I am running Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1) on an Intel Xeon 64bit W3520 2.67GHz
MATLAB Version: 8.1.0.604 (R2013a)

采纳的回答

Roger Stafford
Roger Stafford 2013-9-6
Whatever your matlab version and whatever your operating system, you are attempting to do decimal arithmetic on a machine that uses binary numbers. For that reason it cannot represent 0.008 exactly. For that matter it can represent neither 0.088 nor 0.08 exactly. If you display the number the machine actually uses to sufficiently many digits, you will always be able to see the difference. However (for double precision numbers) the error tends to be out in the fifteenth or sixteenth decimal place. It is something all computer users must become accustomed to.
  1 个评论
Dave
Dave 2013-9-6
Roger,
I have also been looking at http://support.microsoft.com/kb/42980 - this helped me to understand.
Many thanks, Dave

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by