lua-users home
lua-l archive

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


On Thu, Jun 9, 2016 at 8:02 PM, Coda Highland <chighland@gmail.com> wrote:
> Calculation of a^b (both a and b are integers, b is non-negative)

Can we just leave the decisionmaking to the programmer? Add add
math.ipow() or Python's ** operator for integer exponentiation.

Actually, integer exponentiation is absolutely the same operation as float exponentiation.
From math point of view.
Does integer exponentiation really require separate operation symbol?

BTW, there are operations in Lua that need separate symbol a lot more.
For example, addition, subtraction and multiplication.

Integer multiplication:
> a=5000000000
> b=3000000000
> a*b
-3446744073709551616

Float multiplication:
> 1.*a*b
1.5e+19

Operations over reals and over integers modulo 2^64 are very different operations.
And they share the same operation symbols.
IMO, "+", "-" and "*" need programmer's decisionmaking a lot more.

Currently, there is no assurance that the symbol "+" in Lua program will invoke the addition user implied and not the other addition.
I can't come up with a good idea how to solve this.
Non-7-bit-ASCII symbols for operations is a bad idea.
Using "[+]", "(*)" or other complex symbols for integer operations is a bit awkward.
Using bit64 library will make integer operations boring to use.

As for now, Lua 5.3 users must paranoically check that all operations do preserve math.type of values.
Type can be accidentally changed from float to integer by math.floor() or by assigning a numeric literal without ".0" suffix.
Type can be accidentally changed from integer to float by using "/" or "^".
Such mistakes have a good chance to stay unrevealed.