I can not understand why Julia displays such results in chain comparisons?

question
#1

Hello dear friends
I can not understand why Julia displays the following result for the expression of chain comparisons:

julia> v(x) = (println(x); x)
v (generic function with 1 method)

julia> v(1) < v(3) <= v(4) < v(5) < v(10)
3
1
4
5
10
true

Why Julia does not show the following result:

julia> v(1) < v(3) <= v(4) < v(5) < v(10)
1
3
4
5
10
true

Hopefully I mean well. Tell me to explain more if I’m saying bad question.
tnx:slightly_smiling_face:

#2

Did you see:

https://docs.julialang.org/en/v1/manual/mathematical-operations/index.html#Chaining-comparisons-1

1 Like
#3

This might make it a bit more clear how things are executed

julia> f() = v(1) < v(3) <= v(4) < v(5) < v(10);

julia> @code_lowered f()
CodeInfo(
1 ─ %1  = (Main.v)(3)
β”‚   %2  = (Main.v)(1)
β”‚   %3  = %2 < %1
└──       goto #7 if not %3
2 ─ %5  = (Main.v)(4)
β”‚   %6  = %1 <= %5
└──       goto #6 if not %6
3 ─ %8  = (Main.v)(5)
β”‚   %9  = %5 < %8
└──       goto #5 if not %9
4 ─ %11 = (Main.v)(10)
β”‚   %12 = %8 < %11
└──       return %12
5 ─       return false
6 ─       return false
7 ─       return false
)
1 Like
#4

yes i did, but i can’t understand.

#5

The compiler is free to rearrange the order to produce more efficient code, or for any other reason. As it says in the manual, the order is undefined, and you should not depend on it.

#6

I am very beginner and I do not understand anything from the code you wrote.:roll_eyes:

#7

Yes, I read that. But finally, the one who made Julia must know what’s going on. Is not it?