• Subject: Re: Operator precedence and unary minus
• From: Coda Highland <chighland@...>
• Date: Mon, 21 May 2018 16:41:03 -0500

```On Mon, May 21, 2018 at 3:47 PM, Egor Skriptunoff
<egor.skriptunoff@gmail.com> wrote:
> Hi!
>
> In most programming languages (and in Lua too) unary minus has higher
> priority than group of multiplicative operators ( * / % ).
> It usually doesn't cause any problems due to "commutativity" between
> negation and any multiplicative operator in most programming languages:
> (-a) * b == - (a*b)
> (-a) / b == - (a/b)
> (-a) % b == - (a % b)
> As you can see, precedence of unary minus actually doesn't matter when only
> basic arithmetic operators are involved ( + - * / % ).
> We can write (...-a%b) without worrying about precedence of binary / unary
> minus.
>
> But Lua is special.
> Lua has mathematically correct modulo operator (and that's great !).
> The problem arises because in Lua negation operator is "not commutative"
> with modulo:
> (-a) % b ~= - (a%b)
>
> That's why in Lua we have an unpleasant surprise:
> - a%m - b%n ~= - b%n - a%m
> It's looks very weird.
> That should be an equality as in all other programming languages.
>
> I think it would be good to lower the priority of unary minus in Lua.
> For example, unary minus may have the same priority as binary minus.
> What do you think about it?
>
> BTW, the same problem exists for // operator.
>
> -- Egor

That kind of subtle semantic change sounds like a terrible idea. Sure,
mathematically it would be nice, but it could break existing code in a
way that could be very subtle. The benefit doesn't seem to be worth
the risk.