About logical calculation. Is this a bug?

1 Ansicht (letzte 30 Tage)
Jingli Xie
Jingli Xie am 14 Jan. 2020
Kommentiert: Steven Lord am 14 Jan. 2020
3.jpg
what happened to this case? Is this a matlab bug?
  1 Kommentar
Jingli Xie
Jingli Xie am 14 Jan. 2020
logical should be 1 at the lower left corner of the picture.
-1<p_grad == 1, p_grad < 1 ==1, bug -1<p_grad<1 == 0

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 14 Jan. 2020
Bearbeitet: Adam Danz am 14 Jan. 2020
The operation
p_grad = 1.1200e-17;
-1 < p_grad < 1
works from left-to-right. It is interpretted as
p_grad = 1.1200e-17;
(-1 < p_grad) < 1
which is simplified to
1 < 1
which equals 0 (false).
To determine if p_grad is between -1 and 1, (parentheses optional)
(-1 < p_grad) && (p_grad < 1) % for scalar values
(-1 < p_grad) & (p_grad < 1) % for non-scalar values or when
% number of element is unknown
  3 Kommentare
Steven Lord
Steven Lord am 14 Jan. 2020
If I remember correctly, as of release R2019b if you try to use that idiom in a file in the Editor Code Analyzer will warn you that it doesn't do what you think it does.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by