Rounding in Matlab (very trivial)

2 ビュー (過去 30 日間)
Ryan Shrott
Ryan Shrott 2015 年 8 月 19 日
コメント済み: Walter Roberson 2015 年 8 月 19 日
Today is my first day using Matlab. I have been using C++ for last few years. Could someone please explain why FALSE is printed here?
%%Rounding of decimal fractions
x1=0.3
x2=0.1+0.1 + 0.1
test1=(x1==x2) % PRINTS FALSE? WHY?

採用された回答

Steven Lord
Steven Lord 2015 年 8 月 19 日
See question 1 in the Mathematics section of the FAQ.
  3 件のコメント
Image Analyst
Image Analyst 2015 年 8 月 19 日
I suppose for some number combinations it will match all the way out to the umpteenth decimal place and for others it won't. Bottom line: to be robust you need to check non-integer floating point numbers against a tolerance and not for equality.
Walter Roberson
Walter Roberson 2015 年 8 月 19 日
It does not have to be intuitive: it depends upon the exact bit pattern and upon the rounding mode that is in effect.
https://en.wikipedia.org/wiki/Rounding#Round_half_to_even

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by