lua-users home
lua-l archive

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


You seem to have forgotten the presence of NOT :
"NOT a AND b" is not evident in common English where it could mean NOT(a AND b), while programming languages most often mean "(NOT a) AND b". Replace "AND" by "OR", this is the same.

With the additional parentheses at least, you won't be fooled by common English, even if "NOT a AND b" has a single interpretation in most programming languages where unary operators like NOT take precedence to binary operators (which is not completely true in Lua, with the exponentiation operator "^" which has higher precedence than the unary minus "-", so that "-2^2" means "-(2^2)" (i.e. the negative value "-4") and not "(-2)^2" (i.e. the opposite value "+4").

Usually however, we don't need extra parentheses with "(NOT a) AND b" as we are reading a program and not an English book.

Lua however is quite strange with the precedence given to "<<" for bit shifts (wich are comparable to an multiplication with a power of 2), and the concatenation of strings with ".." which has higher precedence than other arithmeric operators "+", "-", "*", "/" (but not the power "^") : this is clearly different from other comparable languages and very counterintuitive, but it forces you to surround your numeric expressions into parentheses before concatenating the string converted from the numeric _expression_.

So you have to write: "0"..(1+2) in Lua to get the string value "03"; with "0"..1+2, you'd get the numeric value 3 and  the _expression_ "x="..1+2 would not return "x=3" but a type mismatch error from an attempt to numerically add the string "x1" (not convertible to a number) and the number 2.

Le jeu. 7 janv. 2021 à 00:47, Egor Skriptunoff <egor.skriptunoff@gmail.com> a écrit :
On Thu, Jan 7, 2021 at 2:30 AM Jonathan Goble wrote:
"and" has precedence over "or". But it's not obvious

It's obvious because AND has precedence over OR in every programming language.