New Lua functions, math.sign and math.clamp

Interesting. :stuck_out_tongue:

Looks good to me. Math.Round would also be very useful.

I think native bit-wise operators could be handy. Particularly for things like custom simplex noise functions.

2 Likes

The C-style ternary is how I expected both to work - why is Lua’s “A and B or C” pattern not functionally equivalent to the ternary operator in C?

Because it’s not a ternary operator. It just happens to somewhat emulate it.

2 Likes

and and or are operations on their own. and comes before or, so a and b or c is the same as (a and b) or c, which might be easier to understand.

2 Likes

Because it’s a pattern people use a lot, since it works most of the time and has the arguments in the same visual order as a C-style ternary, but it’s not the correct logic expression for a 2-to-1 Multiplexer. The more correct logic expression is (A and B) or ((not A) and C) which requires the condition ‘A’ to be written out twice, thus breaking the nice visual similarity with the ? ternary. Also, even this expression won’t let you assign nil, because logic won’t evaluate to nil, only false.

3 Likes

Append “or nil” to turn false into nil.

1 Like

And the expression gets even longer if you want to be able to conditionally return either false or nil. :smile: You have to use true for false, negate it all, and… please no one do this.

1 Like

That expression works fine:

A,B,C = false,1,false
print((A and B) or ((not A) and C)) --> false
A,B,C = false,1,nil
print((A and B) or ((not A) and C)) --> true
-- or "short" notation: A and B or not A and C

Because and just returns the second parameter if the first is truthy and the first paremeter otherwise:

false and 1 --> false
nil and 1 --> nil
true and false --> false
true and nil --> nil

The case for which it does not work is:

A,B,C = true, nil, false --> evaluates to false, desired result is nil

and if you exchange the arguments to the or, making it: ((not A) and C) or (A and B), then it fails

A,B,C = false, false, nil --> evaluates to false, desired result is nil

Want one logic expression that is nil compatible? Maybe try this one:

not ((A and B or not A and C)==nil or (not A and C or A and B)==nil) and (A and B or not A and C) or (((A and B or not A and C)==nil or (not A and C or A and B)==nil) and nil)

If you can come up with a significantly shorter one, I’ll be suitably impressed! :grinning:

If I’m not limited to and/or/not operators:

({B,C})[A and 1 or 2]

but that’s slower than a (not too long) ternary chain.
Otherwise this, that I somehow got:

A and B or not A and C or ((A and B==nil or not A and C==nil) and nil)

Tests: (first column is just whether it’s correct)

true    |       true    1       2       =>      1
true    |       true    1       nil     =>      1
true    |       true    1       false   =>      1
true    |       true    nil     2       =>      nil
true    |       true    nil     nil     =>      nil
true    |       true    nil     false   =>      nil
true    |       true    false   2       =>      false
true    |       true    false   nil     =>      false
true    |       true    false   false   =>      false
true    |       false   1       2       =>      2
true    |       false   1       nil     =>      nil
true    |       false   1       false   =>      false
true    |       false   nil     2       =>      2
true    |       false   nil     nil     =>      nil
true    |       false   nil     false   =>      false
true    |       false   false   2       =>      2
true    |       false   false   nil     =>      nil
true    |       false   false   false   =>      false
2 Likes

Alternatively, if you know B can be false/nil you can just invert it:
not A and C or B

That would work if you also knew C isn’t false/nil (or if C is false/nil, C == B)

2 Likes

I like the compactness of this, it’s readable too! :grinning: But sadly, Lua’s probably one of the languages where this is least performant. If the array is compile-time constant, or at least invariant within a loop and being accessed a lot, it could beat conditionals and even switch statements… in an optimized, compiled language setting.