Matlab precision: simple subtraction is not zero

前端 未结 5 1989
野趣味
野趣味 2021-01-16 13:20

I compute this simple sum on Matlab:

2*0.04-0.5*0.4^2 = -1.387778780781446e-017

but the result is not zero. What can I do?

5条回答
  •  无人及你
    2021-01-16 14:00

    I'm pretty sure this is a case of ye olde floating point accuracy issues.

    Do you need 1e-17 accuracy? Is this merely a case of wanting 'pretty' output? In that case, you can just use a formatted sprintf to display the number of significant digits you want.

    Realize that this is not a matlab problem, but a fundamental limitation of how numbers are represented in binary.

    For fun, work out what .1 is in binary...

    Some references: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems http://www.mathworks.com/support/tech-notes/1100/1108.html

提交回复
热议问题