

Is there any way to enforce integer arithmetic in Lua 5.3
e.g.
x = 5 / 2  => 2
where x is an integer?


On 09/10/2019 04.41, D Burgess wrote:
> Is there any way to enforce integer arithmetic in Lua 5.3
>
> e.g.
>
> x = 5 / 2  => 2
> where x is an integer?
Do you mean 5//2? Or in general?
 nobody


> Is there any way to enforce integer arithmetic in Lua 5.3
>
> e.g.
>
> x = 5 / 2  => 2
> where x is an integer?
1) Do not use '/' or '^'.
2) For any value that enters the computation that you cannot be sure
it is an integer, apply a conversion to it. How to convert depends
on you:
 x  0  fast, raises an error if x is not an integer value
 math.tointeger(x)  returns falsy if x is not an integer value
 math.floor, math.ceil, math.modf
 rounds x (but the result may not be in the proper range)
 Roberto


On Wed, Oct 9, 2019 at 6:02 AM Roberto Ierusalimschy < [hidden email]> wrote: > Is there any way to enforce integer arithmetic in Lua 5.3
>
> e.g.
>
> x = 5 / 2  => 2
> where x is an integer?
1) Do not use '/' or '^'.
2) For any value that enters the computation that you cannot be sure
it is an integer, apply a conversion to it. How to convert depends
on you:
 x  0  fast, raises an error if x is not an integer value
 math.tointeger(x)  returns falsy if x is not an integer value
 math.floor, math.ceil, math.modf
 rounds x (but the result may not be in the proper range)
 Roberto
For my edification: The answer is then "No, there is no way 'in general' to perform integer math"? It seems to me that all the suggestions above are value conversions/coercion and have nothing to do with actually performing the integer math equation requested by the OP?


For all the arithmetic operators(+/*), if your inputs are integers, then your output must be an integer (unless you go outside the representable range). The exception is division, so integer division handles that case. So if you convert all your inputs to integers and use integer division instead of float division, you'll never have a noninteger.
On Wed, Oct 9, 2019 at 6:02 AM Roberto Ierusalimschy < [hidden email]> wrote: > Is there any way to enforce integer arithmetic in Lua 5.3
>
> e.g.
>
> x = 5 / 2  => 2
> where x is an integer?
1) Do not use '/' or '^'.
2) For any value that enters the computation that you cannot be sure
it is an integer, apply a conversion to it. How to convert depends
on you:
 x  0  fast, raises an error if x is not an integer value
 math.tointeger(x)  returns falsy if x is not an integer value
 math.floor, math.ceil, math.modf
 rounds x (but the result may not be in the proper range)
 Roberto
For my edification: The answer is then "No, there is no way 'in general' to perform integer math"? It seems to me that all the suggestions above are value conversions/coercion and have nothing to do with actually performing the integer math equation requested by the OP?


On Wed, 20191009 at 11:48 0400, Katie Volz wrote:
> For all the arithmetic operators(+/*), if your inputs are integers,
> then your output must be an integer (unless you go outside the
> representable range). The exception is division, so integer division
> handles that case. So if you convert all your inputs to integers and
> use integer division instead of float division, you'll never have a
> noninteger.
>
> On Wed, Oct 9, 2019, 11:44 AM Russell Haley < [hidden email]>
> wrote:
> >
> > On Wed, Oct 9, 2019 at 6:02 AM Roberto Ierusalimschy <
> > [hidden email]> wrote:
> > > > Is there any way to enforce integer arithmetic in Lua 5.3
> > > >
> > > > e.g.
> > > >
> > > > x = 5 / 2  => 2
> > > > where x is an integer?
> > >
> > > 1) Do not use '/' or '^'.
> > >
> > > 2) For any value that enters the computation that you cannot be
> > > sure
> > > it is an integer, apply a conversion to it. How to convert
> > > depends
> > > on you:
> > >
> > >  x  0  fast, raises an error if x is not an integer value
> > >
> > >  math.tointeger(x)  returns falsy if x is not an integer
> > > value
> > >
> > >  math.floor, math.ceil, math.modf
> > >  rounds x (but the result may not be in the proper range)
> > >
> > >
> > >  Roberto
> > >
> >
> > For my edification: The answer is then "No, there is no way 'in
> > general' to perform integer math"? It seems to me that all the
> > suggestions above are value conversions/coercion and have nothing
> > to do with actually performing the integer math equation requested
> > by the OP?
> >
If you go outside of integer range, result is still an integer, just
not one you may expect.


For all the arithmetic operators(+/*), if your inputs are integers, then your output must be an integer (unless you go outside the representable range). The exception is division, so integer division handles that case. So if you convert all your inputs to integers and use integer division instead of float division, you'll never have a noninteger.
Okay, so use floor division as outlined in 3.4.1 – Arithmetic Operators. Thanks! Russ
On Wed, Oct 9, 2019 at 6:02 AM Roberto Ierusalimschy < [hidden email]> wrote: > Is there any way to enforce integer arithmetic in Lua 5.3
>
> e.g.
>
> x = 5 / 2  => 2
> where x is an integer?
1) Do not use '/' or '^'.
2) For any value that enters the computation that you cannot be sure
it is an integer, apply a conversion to it. How to convert depends
on you:
 x  0  fast, raises an error if x is not an integer value
 math.tointeger(x)  returns falsy if x is not an integer value
 math.floor, math.ceil, math.modf
 rounds x (but the result may not be in the proper range)
 Roberto
For my edification: The answer is then "No, there is no way 'in general' to perform integer math"? It seems to me that all the suggestions above are value conversions/coercion and have nothing to do with actually performing the integer math equation requested by the OP?


But Lua has no programmatic way to specify that a number whose value is an integer must be treated as an integer or as a float
(both have typeof == "number")
. Integers are just internal optimisations for the fast path. With the same exposed type, you would not be able to compute the price per liter of a box containing exactly 2 liters of cream and costing exactly 5 dollars: if you display 2 $/L, your price is wrong when the effective cost is 2.50 $/L. So:  either we have separate datatypes (instead of just internal subtypes): this is what you do in C, C++, Java...  or we need separate operations (operators or functions): this is what you do in Lua, PHP, Javascript and in classic mathematics with an unified number type...  or we need to specify an operating mode to specify the semantics : this is the worst, the least manageable as this makes the code highly dependant on context (including in subfunctions that would not be aware and would be expected to be called and have been tested only from one mode): this is similar to the IEEE rounding modes, difficult to track and isolate in C/C++, and banned in Java, or being effective only in the local scope (creating difficulties when the code is refactored with subfunctions).
This problem is not solvable without one of these choices.
The safest solution IMHO is to have separate operations, so "/" should not enforce the integeronly arithmetic and should preserve the maximum precision supported for ALL valid number values.
For all the arithmetic operators(+/*), if your inputs are integers, then your output must be an integer (unless you go outside the representable range). The exception is division, so integer division handles that case. So if you convert all your inputs to integers and use integer division instead of float division, you'll never have a noninteger.
Okay, so use floor division as outlined in 3.4.1 – Arithmetic Operators. Thanks! Russ
On Wed, Oct 9, 2019 at 6:02 AM Roberto Ierusalimschy < [hidden email]> wrote: > Is there any way to enforce integer arithmetic in Lua 5.3
>
> e.g.
>
> x = 5 / 2  => 2
> where x is an integer?
1) Do not use '/' or '^'.
2) For any value that enters the computation that you cannot be sure
it is an integer, apply a conversion to it. How to convert depends
on you:
 x  0  fast, raises an error if x is not an integer value
 math.tointeger(x)  returns falsy if x is not an integer value
 math.floor, math.ceil, math.modf
 rounds x (but the result may not be in the proper range)
 Roberto
For my edification: The answer is then "No, there is no way 'in general' to perform integer math"? It seems to me that all the suggestions above are value conversions/coercion and have nothing to do with actually performing the integer math equation requested by the OP?

