Problem with integer division in Lua 5.3.1

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

Problem with integer division in Lua 5.3.1

Howard

From page 18 of a PDF at http://www.lua.org/wshop14/Ierusalimschy.pdf, Professor Ierusalimschy states: "Integer division converts operands to integers and does an integer division."

However, the following examples using Lua 5.3.1 interactively, show that either there is something about this I don't understand, or the preceding statement is not correct:

C:\>lua
Lua 5.3.1  Copyright (C) 1994-2015 Lua.org, PUC-Rio

> 1//1
1
> 3.0//2
1.0

> 3.0 // 3.0
1.0
> 3 // 2
1
> 2.0//2
1.0
> 2 // 2.0
1.0
> 6.0 // 1
6.0

These examples show that the operands of // are *not* converted to integer, and in the case of non-integer operands, // does not do an integer division unless both operands are in integer form.

Am I just misunderstanding something basic, or is this actually a bug?


Howard Lee Harkness
Reply | Threaded
Open this post in threaded view
|

Re: Problem with integer division in Lua 5.3.1

Roberto Ierusalimschy
> >From page 18 of a PDF at http://www.lua.org/wshop14/Ierusalimschy.pdf,
> Professor Ierusalimschy states: "Integer division converts operands to
> integers and does an integer division."
>
> However, the following examples using Lua 5.3.1 interactively, show that
> either there is something about this I don't understand, or the preceding
> statement is not correct:
>
> C:\>lua
> Lua 5.3.1  Copyright (C) 1994-2015 Lua.org, PUC-Rio
>
> > 1//1
> 1
> > 3.0//2
> 1.0
>
> > 3.0 // 3.0
> 1.0
> > 3 // 2
> 1
> > 2.0//2
> 1.0
> > 2 // 2.0
> 1.0
> > 6.0 // 1
> 6.0
>
> These examples show that the operands of // are *not* converted to integer,
> and in the case of non-integer operands, // does not do an integer division
> unless both operands are in integer form.
>
> Am I just misunderstanding something basic, or is this actually a bug?

That talk was given before Lua 5.3 was released, and some details in
those slides were outdated.  Between the talk and the release, the
operation changed from "integer division" to "floor division".

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Problem with integer division in Lua 5.3.1

Howard
In reply to this post by Howard
I did uncover a misunderstanding that I had in my last email.

C:\>lua
Lua 5.3.1  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> 2 // 3
0
> 63 // 4
15
> 63 // 4.0
15.0
> 64 // 4.0
16.0
> 63.0 // 4.0
15.0
> 63.0 / 4
15.75
> 63 / 4
15.75
> 63.1 // 4.0
15.0

This snippet shows that // does indeed do integer division, and does appear to convert the operands to integer. But in the case that one or both of the operands are formatted as floats, the result is formatted as a float. The result always has a ".0" appended, which threw me off initially.

So this appears to be more of a "quirk" than a "bug."


Howard Lee Harkness
<a href="tel:918-416-1420" value="+19184161420" target="_blank">918-416-1420

On Wed, Nov 25, 2015 at 9:40 AM, Howard Lee Harkness <[hidden email]> wrote:

From page 18 of a PDF at http://www.lua.org/wshop14/Ierusalimschy.pdf, Professor Ierusalimschy states: "Integer division converts operands to integers and does an integer division."

However, the following examples using Lua 5.3.1 interactively, show that either there is something about this I don't understand, or the preceding statement is not correct:

C:\>lua
Lua 5.3.1  Copyright (C) 1994-2015 Lua.org, PUC-Rio

> 1//1
1
> 3.0//2
1.0

> 3.0 // 3.0
1.0
> 3 // 2
1
> 2.0//2
1.0
> 2 // 2.0
1.0
> 6.0 // 1
6.0

These examples show that the operands of // are *not* converted to integer, and in the case of non-integer operands, // does not do an integer division unless both operands are in integer form.

Am I just misunderstanding something basic, or is this actually a bug?


Howard Lee Harkness

Reply | Threaded
Open this post in threaded view
|

Re: Problem with integer division in Lua 5.3.1

Howard
In reply to this post by Roberto Ierusalimschy

Just saw this.

I guess that clears up my misunderstanding, but I still regard an operation called "integer division" which sometimes returns a result of type "float" to be counterintuitive. Fortunately, it does not appear to matter for the application I'm currently working on. Plus, now that I know about this quirk, I can work around it.

On Nov 25, 2015 10:06, "Roberto Ierusalimschy" <[hidden email]> wrote:
> >From page 18 of a PDF at http://www.lua.org/wshop14/Ierusalimschy.pdf,
> Professor Ierusalimschy states: "Integer division converts operands to
> integers and does an integer division."
>
> However, the following examples using Lua 5.3.1 interactively, show that
> either there is something about this I don't understand, or the preceding
> statement is not correct:
>
> C:\>lua
> Lua 5.3.1  Copyright (C) 1994-2015 Lua.org, PUC-Rio
>
> > 1//1
> 1
> > 3.0//2
> 1.0
>
> > 3.0 // 3.0
> 1.0
> > 3 // 2
> 1
> > 2.0//2
> 1.0
> > 2 // 2.0
> 1.0
> > 6.0 // 1
> 6.0
>
> These examples show that the operands of // are *not* converted to integer,
> and in the case of non-integer operands, // does not do an integer division
> unless both operands are in integer form.
>
> Am I just misunderstanding something basic, or is this actually a bug?

That talk was given before Lua 5.3 was released, and some details in
those slides were outdated.  Between the talk and the release, the
operation changed from "integer division" to "floor division".

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Problem with integer division in Lua 5.3.1

Roberto Ierusalimschy
> I guess that clears up my misunderstanding, but I still regard an operation
> called "integer division" which sometimes returns a result of type "float"
> to be counterintuitive.

As I explained a few messages ago, the operation is not called integer
division. It is called floor division.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Problem with integer division in Lua 5.3.1

Ignacio Burgueño-2
On Sat, Nov 28, 2015 at 12:34 PM, Roberto Ierusalimschy <[hidden email]> wrote:

As I explained a few messages ago, the operation is not called integer
division. It is called floor division.

Roberto, is it worth changing that in the readme too (the 'Language' section)? 


It says "integer division" there.


Reply | Threaded
Open this post in threaded view
|

Re: Problem with integer division in Lua 5.3.1

Roberto Ierusalimschy
> Roberto, is it worth changing that in the readme too (the 'Language'
> section)?
>
> http://www.lua.org/manual/5.3/readme.html#changes
>
> It says "integer division" there.

Sure.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Problem with integer division in Lua 5.3.1

Nagaev Boris
In reply to this post by Ignacio Burgueño-2
On Mon, Nov 30, 2015 at 6:14 PM, Ignacio Burgueño <[hidden email]> wrote:

> On Sat, Nov 28, 2015 at 12:34 PM, Roberto Ierusalimschy
> <[hidden email]> wrote:
>>
>>
>> As I explained a few messages ago, the operation is not called integer
>> division. It is called floor division.
>
>
> Roberto, is it worth changing that in the readme too (the 'Language'
> section)?
>
> http://www.lua.org/manual/5.3/readme.html#changes
>
> It says "integer division" there.
>
>

It says "floor division" here.


--


Best regards,
Boris Nagaev

Reply | Threaded
Open this post in threaded view
|

Re: Problem with integer division in Lua 5.3.1

Jonathan Goble
On Tue, Dec 1, 2015 at 5:26 PM, Nagaev Boris <[hidden email]> wrote:
> On Mon, Nov 30, 2015 at 6:14 PM, Ignacio Burgueño <[hidden email]> wrote:
>> http://www.lua.org/manual/5.3/readme.html#changes
>>
>> It says "integer division" there.
>
> It says "floor division" here.

It was changed. Having previously opened the link when Ignacio first
posted it, I got "integer division" again when I opened it just now.
After bypassing my cache [1], I got the updated version that says
"floor division".

[1] https://en.wikipedia.org/wiki/WP:BYPASS

Reply | Threaded
Open this post in threaded view
|

Re: Problem with integer division in Lua 5.3.1

Hartmut Henkel
In reply to this post by Roberto Ierusalimschy
On Sat, 28 Nov 2015, Roberto Ierusalimschy wrote:

> > I guess that clears up my misunderstanding, but I still regard an
> > operation called "integer division" which sometimes returns a result
> > of type "float" to be counterintuitive.
>
> As I explained a few messages ago, the operation is not called integer
> division. It is called floor division.

Just to report as a caveat, floor division hit me when quickly
converting a calendar date into a julian date by the formula (Fliegel,
van Flandern):

jd = d - 32075 + 1461 * (y + 4800 + (m - 14) / 12) / 4 + 367 * (m - 2 -
(m - 14) / 12 * 12) / 12 - 3 * ((y + 4900 + (m - 14) / 12) / 100) / 4;

This works flawlessly in Fortran and C, with integers. So i thought, in
Lua assure that variables are integers, then simply replace "/" by "//"
and the result would be fine. However it wasn't since Lua by "floor
division" handles negative divisions like (m - 14) / 12 differently from
the established Fortran and C methods. And it's rather tedious to
rewrite the simple formula above with Lua's integer facility. Fortran
seems to do do essentially this:

local idiv = function(a, b)
  if (a >= 0) and (b >= 0) then return a // b
  elseif (a >= 0) and (b < 0) then return -(a // (-b))
  elseif (a < 0) and (b >= 0) then return -((-a) // b)
  elseif (a < 0) and (b < 0) then return (-a) // (-b)
  else assert(0)
  end
end

More on such cases see also an interesting comparison between languages:
https://craftofcoding.files.wordpress.com/2013/07/cs_langjuliandates.pdf

(i would have liked Lua to do "//" like Fortran does integer division.)

Regards, Hartmut

Reply | Threaded
Open this post in threaded view
|

Re: Problem with integer division in Lua 5.3.1

Daurnimator
On 8 January 2016 at 08:23, Hartmut Henkel <[hidden email]> wrote:

> On Sat, 28 Nov 2015, Roberto Ierusalimschy wrote:
>
>> > I guess that clears up my misunderstanding, but I still regard an
>> > operation called "integer division" which sometimes returns a result
>> > of type "float" to be counterintuitive.
>>
>> As I explained a few messages ago, the operation is not called integer
>> division. It is called floor division.
>
> Just to report as a caveat, floor division hit me when quickly
> converting a calendar date into a julian date by the formula (Fliegel,
> van Flandern):
>
> jd = d - 32075 + 1461 * (y + 4800 + (m - 14) / 12) / 4 + 367 * (m - 2 -
> (m - 14) / 12 * 12) / 12 - 3 * ((y + 4900 + (m - 14) / 12) / 100) / 4;
>
> This works flawlessly in Fortran and C, with integers. So i thought, in
> Lua assure that variables are integers, then simply replace "/" by "//"
> and the result would be fine. However it wasn't since Lua by "floor
> division" handles negative divisions like (m - 14) / 12 differently from
> the established Fortran and C methods. And it's rather tedious to
> rewrite the simple formula above with Lua's integer facility. Fortran
> seems to do do essentially this:
>
> local idiv = function(a, b)
>   if (a >= 0) and (b >= 0) then return a // b
>   elseif (a >= 0) and (b < 0) then return -(a // (-b))
>   elseif (a < 0) and (b >= 0) then return -((-a) // b)
>   elseif (a < 0) and (b < 0) then return (-a) // (-b)
>   else assert(0)
>   end
> end
>
> More on such cases see also an interesting comparison between languages:
> https://craftofcoding.files.wordpress.com/2013/07/cs_langjuliandates.pdf
>
> (i would have liked Lua to do "//" like Fortran does integer division.)
>
> Regards, Hartmut
>

The wikipedia article on modulo has a good bit on the different types
of division+modulo:
https://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation
The image https://en.wikipedia.org/wiki/Modulo_operation#/media/File:Divmod.svg
is also quite helpful to explain why lua is the way it is.

See also, Hisham's blog post:
http://hisham.hm/2015/04/18/a-small-practical-example-where-luas-behavior-is-better-than-cs/