if statement question on two variables

조회 수: 1 (최근 30일)
John W. Z
John W. Z 2016년 6월 22일
댓글: Steven Lord 2016년 6월 22일
there are two variables, f_x2(k)(1*50 double matrix, k=30), f_gbest all are equal to 7.9597,
but "f_x2(k) < f_gbest" returns TRUE!!!
Command History:
>> f_x2(k)
ans =
7.9597
>> f_gbest
f_gbest =
7.9597
>> f_x2(k) < f_gbest
ans =
1
Why returns 1 ? Shouldn't it be 0?

답변 (1개)

Roger Stafford
Roger Stafford 2016년 6월 22일
Try this:
format hex
f_x2(k)
f_gbest
You might be surprised by the result. Or else you can do this:
fprintf(f_x2(k) = %25.20f f_gbest = %25.20f\n’,f_x2(k),f_gbest)
  댓글 수: 1
Steven Lord
Steven Lord 2016년 6월 22일
Or just look at the quantity
delta = f_gbest - f_x2(k)
The value stored in delta will be small but positive. Just because the number is only displayed with a small number of decimal places doesn't mean it's stored with only that small number of decimal places. To display more decimal places, see the help for the format function.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by