# Problem with integer division in Lua 5.3.1

11 messages
Open this post in threaded view
|

## Problem with integer division in Lua 5.3.1

 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:\>luaLua 5.3.1  Copyright (C) 1994-2015 Lua.org, PUC-Rio > 1//11> 3.0//21.0 > 3.0 // 3.01.0> 3 // 21> 2.0//21.0> 2 // 2.01.0> 6.0 // 16.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
Open this post in threaded view
|

## Re: Problem with integer division in Lua 5.3.1

 > >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
Open this post in threaded view
|

## Re: Problem with integer division in Lua 5.3.1

 In reply to this post by Howard I did uncover a misunderstanding that I had in my last email.C:\>luaLua 5.3.1  Copyright (C) 1994-2015 Lua.org, PUC-Rio> 2 // 30> 63 // 415> 63 // 4.015.0> 64 // 4.016.0> 63.0 // 4.015.0> 63.0 / 415.75> 63 / 415.75> 63.1 // 4.015.0This 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 Harkness918-416-1420 On Wed, Nov 25, 2015 at 9:40 AM, Howard Lee Harkness 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:\>luaLua 5.3.1  Copyright (C) 1994-2015 Lua.org, PUC-Rio > 1//11> 3.0//21.0 > 3.0 // 3.01.0> 3 // 21> 2.0//21.0> 2 // 2.01.0> 6.0 // 16.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
Open this post in threaded view
|

## Re: Problem with integer division in Lua 5.3.1

 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
Open this post in threaded view
|

## Re: Problem with integer division in Lua 5.3.1

 > 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
Open this post in threaded view
|

## Re: Problem with integer division in Lua 5.3.1

 On Sat, Nov 28, 2015 at 12:34 PM, Roberto Ierusalimschy 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.
Open this post in threaded view
|

## Re: Problem with integer division in Lua 5.3.1

 > 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
Open this post in threaded view
|

## Re: Problem with integer division in Lua 5.3.1

 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
Open this post in threaded view
|

## Re: Problem with integer division in Lua 5.3.1

 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