필터 지우기
필터 지우기

1.0010 DOES NOT EQUAL 1.0010

조회 수: 4 (최근 30일)
Jeff
Jeff 2011년 11월 10일
Hi all,
I have a segment of code that is evaluating curfactor(sc) == 1.0010 as 0 when curfactor(sc) == 1.0010 (should evaluate as 1). Both numbers are exactly 1.0010. I checked the class of both numbers and they are both double. What on Earth is happening here?!
I should note that curfactor is an array of size 1x3, and is defined at the very beginning of the function. The expression that is being evaluated is nested within 2 other helper functions. Maybe it's a scope issue? In any case, someone's breath stinks here, and it's certainly not mine.
Jeff

채택된 답변

Image Analyst
Image Analyst 2011년 11월 10일
Jeff: Read the FAQ and then come back: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F Like you said, it IS a scope issue, and I'd recommend Scope mouthwash for you - it's an excellent mouthwash.
  댓글 수: 1
Jeff
Jeff 2011년 11월 10일
Cheers. I will use a tolerance value instead of making a direct comparison.
Readers Beware: I used the round2 function from the Mathworks File Exchange to obtain a number of arbitrary precision and stored it in curfactor(sc). When using this function, take caution that the number is not rounded exactly to the precision specified, there may be hidden bits.

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

추가 답변 (2개)

bym
bym 2011년 11월 10일
did you check that they were exactly equal with a variable precision package like the symbolic toolbox?
  댓글 수: 2
Teja Muppirala
Teja Muppirala 2011년 11월 10일
Using "format hex" will also show the difference:
format hex
X = [(0.1+0.2); 0.3]
Jeff
Jeff 2011년 11월 10일
Thanks for the tip!

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


Friedrich
Friedrich 2011년 11월 10일
  댓글 수: 1
Jeff
Jeff 2011년 11월 10일
Thanks, I'll have a look at it.

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

카테고리

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