Imprecise Basic operations

조회 수: 2 (최근 30일)
Philipp
Philipp 2011년 7월 13일
Hi everyone,
I am using Matlab 2009a. When I try to calculate
0.9 - 0.8 - 0.1
then the result is
-2.775557561562891 e-017
Close to zero, but not zero. Is it not possible to get a precise solution for a floating point operation? This minor imprecision has large implications for my programs.
Is there a way to get precise calculations? Why does matlab get this (quite easy task) wrong?
Thanks for your help!
  댓글 수: 1
James Tursa
James Tursa 2011년 7월 13일
You might find this FEX submission useful:
http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str

댓글을 달려면 로그인하십시오.

채택된 답변

Nathan Greco
Nathan Greco 2011년 7월 13일
Welcome to the world of floating point computing.
A computer can't exactly represent most floating point numbers.
Example: Try calculating 3*(1/3) by hand, with writing out 1/3 to as many decimal places as you please. You will get .9999..., which is CLOSE to 1, but is not equal to 1 (which the true answer should be).
  댓글 수: 2
Jan
Jan 2011년 7월 13일
+1: Exactly. The behaviour is neither "wrong" nor "inprecise". It simply demonstrates the limited precision.
Walter Roberson
Walter Roberson 2011년 7월 13일
Though if you go an infinite number of decimal places, 0.99999999999999.... (with infinite 9's) *is* equal to 1.
If you had an infinite number of bits, you could get an exact binary representation for 0.1 -- but of course no physically realizable computer can be infinite.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Philipp
Philipp 2011년 7월 14일
Thanks for your quick responses. I was aware of the numerical issues when calculation 1/3 * 3, or using non-rational numbers. But I was not aware of the fact that the binary representation of 0.1 has a infinite number of digits. Thanks.

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by