Fwd: Lua 5.3.4 Floor division raise error

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: Lua 5.3.4 Floor division raise error

Guislain Duthieuw
(I'm not sure if this email has been received, nor if it must be sent here.)

In Lua 5.3.4 the following code raise an error
> =1//0
stdin:1: attempt to divide by zero
stack traceback:
    stdin:1: in main chunk
    [C]: in ?

But the following code works like division
> =1//0.0
inf

This seems inconsistent between division and floor division,
but it check the test... that describe it explicitely
See :

I would suggest to make 1//0 returns inf as well



Guile

Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Lua 5.3.4 Floor division raise error

Andrew Gierth
>>>>> "Guislain" == Guislain Duthieuw <[hidden email]> writes:

 Guislain> This seems inconsistent between division and floor division,

Obviously because normal division is always returning a float, whereas
floor division between integers returns an integer.

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Lua 5.3.4 Floor division raise error

Guislain Duthieuw
During that time : I have found the same thing with modulo %

> Obviously because normal division is always returning a float, whereas
> floor division between integers returns an integer.

I am not a Lua expert but I tested this :
> =10.0//3
3.0

Does that show floor division can return a float ? (as the manual says I think)


On Wed, Feb 14, 2018 at 12:07 PM, Andrew Gierth <[hidden email]> wrote:
>>>>> "Guislain" == Guislain Duthieuw <[hidden email]> writes:

 Guislain> This seems inconsistent between division and floor division,

Obviously because normal division is always returning a float, whereas
floor division between integers returns an integer.

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Lua 5.3.4 Floor division raise error

Andrew Gierth
>>>>> "Guislain" == Guislain Duthieuw <[hidden email]> writes:

 >> Obviously because normal division is always returning a float,
 >> whereas floor division between integers returns an integer.

 Guislain> I am not a Lua expert but I tested this :
 Guislain> =10.0//3
 Guislain> 3.0

10.0 is a float.

 Guislain> Does that show floor division can return a float ? (as the
 Guislain> manual says I think)

The manual says (emphasis mine):

    With the exception of exponentiation and float division, the
    arithmetic operators work as follows: IF BOTH OPERANDS ARE INTEGERS,
    the operation is performed over integers and the result is an
    integer. OTHERWISE, if both operands are numbers or strings that can
    be converted to numbers (see §3.4.3), then they are converted to
    floats, the operation is performed following the usual rules for
    floating-point arithmetic (usually the IEEE 754 standard), and THE
    RESULT IS A FLOAT.

    Exponentiation and float division (/) always convert their operands
    to floats and the result is always a float.

So 10.0//3 does not have both operands as integers so it is performed
over floats. But 10//3 has both integer operands and therefore an
integer result.

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Lua 5.3.4 Floor division raise error

Guislain Duthieuw
OK, I see what you mean ! (I rephrase your answer)
The error is due to the fact 1//0 WILL return a integer and "inf" is a float.
While 1/0 WILL returns a float and "inf" is available.

Thank you for your explanation.