채택된 답변

Birdman
Birdman 2018년 4월 2일
편집: Birdman 2018년 4월 2일

0 개 추천

A trivial answer:
Use ismembertol for comparing values with tolerance
tol=1e-6;
if ismembertol(t,0.05,tol)

추가 답변 (1개)

John D'Errico
John D'Errico 2018년 4월 2일

1 개 추천

NEVER test for exact equality of a floating point number.
Since doubles are stored in binary, remember that almost all decimal fractions cannot be represented exactly as a double, just like you cannot represent 1/3 exactly as a terminating decimal in a limited number of decimal digits. Doubles use 52 binary bits to store the mantissa, so that is your limit.
Instead, 0.05 is stored as the closest value possible. So never test for exact equality. Just use a tolerance.
if abs(t - 0.05) < 1e-16

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

질문:

2018년 4월 2일

편집:

2018년 4월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by