2

-3<-2<-1 returns True.

However I would expect it interpreted as

(-3<-2)<-1
True<-1
1<-1
False

How is that possible ?

fffred
  • 596
  • 4
  • 19

3 Answers3

7

This is a chained comparison. Instead of being left-associative like (-3 < -2) < -1 or right-associative like -3 < (-2 < -1), it's actually treated as

(-3 < -2) and (-2 < -1)

except that -2 is evaluated at most once.

user2357112
  • 260,549
  • 28
  • 431
  • 505
3

From the docs:

Unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

Therefore

-3 < -2 < -1  

is equivalent to

-3 < -2 and -2 < -1  # where -2 is evaluated only once
TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
0

It's stated in the documentation that it is part of the language

willwolfram18
  • 1,747
  • 13
  • 25