Why does my if statement do the opposite?

8 ビュー (過去 30 日間)
Erwin Avendaño
Erwin Avendaño 2017 年 11 月 8 日
コメント済み: David Goodmanson 2017 年 11 月 8 日
My if statement is asking that if h is negative, you must do that operation if not the other but do the opposite, do not I understand?
t=b4
e=b3
f=b2
h=e^2-4*t*f
if h==-h
qc=3
p=(h)*(-1)
x5=(sqrt(h))/(2*t);
x6=(-e/(2*t));
x7=(-sqrt(h))/(2*t);
x8=(-h/(2*t));
else
qd=4
x5=(-e+sqrt(h))/(2*t);
x6=(-e-sqrt(h))/(2*t);
end

採用された回答

David Goodmanson
David Goodmanson 2017 年 11 月 8 日
Hi Erwin
The test h==-h is not a test for whether h is negative. This test is the same as 2*h==0 and always fails unless h = 0. Take a look at h<0, or h<=0 if you intend the condition to work for h=0 as well as negative h.
  3 件のコメント
Erwin Avendaño
Erwin Avendaño 2017 年 11 月 8 日
Is these two conditions okay? if (qb==2 && qd==4)
David Goodmanson
David Goodmanson 2017 年 11 月 8 日
They are the right syntax anyway, assuming that qb and qd are scalars. I tend to just use & in every situation because it always works and I don't think it has slowed things down for what I ordinarily do.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResults, Reporting, and Test File Management についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by