lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


maths define the concept of "left associativity' (which is not the same as the full "associativity" defined for addition or multiplication). No need to refer to execution, only associativity matters and defines clearly where parentheses are not needed. But substraction, division, exponentiation are not fully associative: they all are left-associative only and this is sufficient for our purpose, so that any grouping of operands of binary operations, when there are more than 2 will require parentheses if the grouping is not using the left-associativity.

So
   1*2*3 == (1*2)*3 == 1*(2*3) : full associativity, true in maths, not necessarily true in programming languages for all operand values (e.g. with float/double types, because of intermediate roundings)
   1/2/3 = (1/2)/3 != 1/(2/3) == 3/2 : left-associativity only

Lua respects this... except for exponentiation where it defaults to right-associativity, which is quite uncommon (if we note it with the "^" operator on formulas written on a single baseline, like most other languages do when having a power operator like "^" or "**"; this excludes C/C++ and Java that don't have them but require using a functional "pow(x, y)" notation).

But it would be more natural if we use superscripts and we "stack" them at different vertical levels: when we don't stack the superscripts, maths formula require parentheses for the left-associativity. However Lua does not support superscript notations.



Le mer. 13 janv. 2021 à 20:40, bel <bel2125@gmail.com> a écrit :

> Yes, "a/b/c/d" is not a valid complex fraction, so an engineer should not try to interpret it this way.

To come back to the original topic, a recommendation for  "when to put parentheses for code readability", I see three options:

a) have a list of people who are "allowed to interpret" and "not allowed to interpet"
b) always have a table of operator precedence at hand, or even type it in the comment next to the code
c) use parentheses at everything that could be mis-interpreted

I will definitely go for option c)

d) Give everybody a brief Lua programming introduction, where you do not assume these things are obvious

... there might be even better options, of course

> But "a÷b÷c÷d" is well defined, because division and multiplication are executed from left to right, every schoolkid knows it.

After some investigation, I have to disagree.
You might find this article interesting: https://math.berkeley.edu/~gbergman/misc/numbers/ord_ops.html
Apparently I'm not the only one who encountered this ambiguity.

Also (my) algebra books define commutativity, associativity, distributivity ... but never "left to right execution".
I NEVER heard of a left to right execution rule in math (only in programming), not at school, not at university.
I am not aware of any university grade algebra book (for mathematics students ... not programming books) that defines "left to right execution order".
I mean a book on fundamental algebra, not "this is correct because a scientific calculation program calculates it this way" because this would be circular reasoning.
Could you name a reference here?




On Wed, Jan 13, 2021 at 11:48 AM Egor Skriptunoff <egor.skriptunoff@gmail.com> wrote:
On Wed, Jan 13, 2021 at 12:21 PM bel wrote:
Wikipedia (https://en.wikipedia.org/wiki/Fraction#Complex_and_compound_fractions):
"So 5/10/20/40 is not a valid mathematical _expression_, because of multiple possible interpretation."

Yes, "a/b/c/d" is not a valid complex fraction, so an engineer should not try to interpret it this way.
But "a÷b÷c÷d" is well defined, because division and multiplication are executed from left to right, every schoolkid knows it.