If you type the this question in command line of MATLAB, you'll probably get the answer other than zero.

5.5511e-017

The guyes work for MATLAB wrote on their blog and explained why.

### Ken & Mike on the MATLAB Desktop

And the lady even insists that " it's not an error". How ridiculous!

### Loren on the Art of MATLAB

*Hugh-*

*This is not an error. If MATLAB were built to get that answer to be 0, we’d have to do a bunch of operations to sort the data in the “right” order. How would MATLAB actually know that IS the way the values should be accumulated from the real-world? You can have MATLAB perform calculations in whatever order you wish by using parentheses. Or introduce a function that sorts the data appropriately and then does the sums and differences.*

*As long as there is no way to perfectly represent some values, floating point calculations are subject to round-off error. As for the other languages, it depends on how the code is written so they get exactly 0. If the constants are built into the program rather than user-supplied at run-time, it’s possible that a C++ compiler will reorder the operations on constants. And Mathematica might be doing the calculations using extended precision (but I don’t know that for certain).*

*–Loren*

Anyway, it seems that no solution will be provided for this "problem", so I'd better keep that in mind if I need to use *if (0.3==0.1*3)* this kind of statement in my program.

What a great team of MATLAB!

## 沒有留言:

張貼留言