Bug in LNUM patch

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

Bug in LNUM patch

Flemming Madsen-3
Hi there

I think i have found a bug.

The LNUM patch changes the test below from lua_isnumber() to
lua_isinteger() in luaL_checkinteger(). However this has the
unfortunate side effect of causing Lua to claim that numbers between
-1.0 < x < 1.0 can not be integers:


> return string.format("%d", 0.999)
stdin:1: bad argument #2 to 'format' (integer expected, got number)
stack traceback:
        [C]: in function 'format'
        stdin:1: in main chunk
        [C]: ?
> return string.format("%d", 1.0001)
Printing 1 values:
1
>
> return string.format("%d", -0.999)
stdin:1: bad argument #2 to 'format' (integer expected, got number)
stack traceback:
        [C]: in function 'format'
        stdin:1: in main chunk
        [C]: ?
> return string.format("%d", -0.0001)
stdin:1: bad argument #2 to 'format' (integer expected, got number)
stack traceback:
        [C]: in function 'format'
        stdin:1: in main chunk
        [C]: ?
> return string.format("%d", -1.0001)
Printing 1 values:
-1
>


The patch below changes this behaviour back again.

Rgds /Flemming

--- lauxlib.c.orig 2010-04-21 17:34:48.000000000 +0200
+++ lauxlib.c 2010-05-11 10:59:23.000000000 +0200
@@ -188,7 +188,7 @@

 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
   lua_Integer d = lua_tointeger(L, narg);
-  if (d == 0 && !lua_isinteger(L, narg))  /* avoid extra test when d
is not 0 */
+  if (d == 0 && !lua_isnumber(L, narg))  /* avoid extra test when d is not 0 */
     tag_error(L, narg, -1 /*integer*/);
   return d;
 }
Reply | Threaded
Open this post in threaded view
|

Re: Bug in LNUM patch

Richard Hundt
Flemming Madsen wrote:
> Hi there
>
> I think i have found a bug.
>
> The LNUM patch changes the test below from lua_isnumber() to
> lua_isinteger() in luaL_checkinteger(). However this has the
> unfortunate side effect of causing Lua to claim that numbers between
> -1.0 < x < 1.0 can not be integers:

Integers are defined as a subset of real numbers that are *whole* (i.e
which can be written without a fractional or decimal component). Numbers
between -1.0 < x < 1.0 therefore *cannot* be integers, so unless I've
misunderstood, the existing behaviour was exactly what you wanted there.

http://en.wikipedia.org/wiki/Integer

Cheers,
Rich
Reply | Threaded
Open this post in threaded view
|

Re: Bug in LNUM patch

Luiz Henrique de Figueiredo
> Numbers between -1.0 < x < 1.0 therefore *cannot* be integers

Unless x=0.
Reply | Threaded
Open this post in threaded view
|

Re: Bug in LNUM patch

Richard Hundt
Luiz Henrique de Figueiredo wrote:
>> Numbers between -1.0 < x < 1.0 therefore *cannot* be integers
>
> Unless x=0.
>

Missed that, thanks :)
Reply | Threaded
Open this post in threaded view
|

Re: Bug in LNUM patch

Flemming Madsen-3
In reply to this post by Richard Hundt
I already knew that, thanks.

The practical implication however is that -1.0 < x < 1.0 can not be
*cast* to integers. At least not by the %d operator of string.format()

On Wed, May 12, 2010 at 4:58 AM, Richard Hundt <[hidden email]> wrote:

> Integers are defined as a subset of real numbers that are *whole* (i.e which
> can be written without a fractional or decimal component). Numbers between
> -1.0 < x < 1.0 therefore *cannot* be integers, so unless I've misunderstood,
> the existing behaviour was exactly what you wanted there.
>
> http://en.wikipedia.org/wiki/Integer
>
> Cheers,
> Rich
>
Reply | Threaded
Open this post in threaded view
|

Re: Bug in LNUM patch

David Kastrup
Flemming Madsen <[hidden email]> writes:

>> On Wed, May 12, 2010 at 4:58 AM, Richard Hundt <[hidden email]> wrote:
>>
>>> Integers are defined as a subset of real numbers that are *whole* (i.e which
>>> can be written without a fractional or decimal component). Numbers between
>>> -1.0 < x < 1.0 therefore *cannot* be integers, so unless I've misunderstood,
>>> the existing behaviour was exactly what you wanted there.
>>>
>>> http://en.wikipedia.org/wiki/Integer
>
> I already knew that, thanks.
>
> The practical implication however is that -1.0 < x < 1.0 can not be
> *cast* to integers. At least not by the %d operator of string.format()

There is no difference between 0 and 0.0 for Lua.

--
David Kastrup

Reply | Threaded
Open this post in threaded view
|

Re: Bug in LNUM patch

Flemming Madsen-3
This is about Lua *with* the lnum patch (And I know, there's still no
difference)

This is all beside the point. The whole thing was brought to my
attention by the fact that string.format("%d", 1.00001) was ok while
string.format("%d", 0.999999) would give an error.

This what the sample Lua session in my first post tried to show.

Cheers /Flemming


On Sun, May 16, 2010 at 11:43 AM, David Kastrup <[hidden email]> wrote:

> Flemming Madsen <[hidden email]> writes:
>
>>> On Wed, May 12, 2010 at 4:58 AM, Richard Hundt <[hidden email]> wrote:
>>>
>>>> Integers are defined as a subset of real numbers that are *whole* (i.e which
>>>> can be written without a fractional or decimal component). Numbers between
>>>> -1.0 < x < 1.0 therefore *cannot* be integers, so unless I've misunderstood,
>>>> the existing behaviour was exactly what you wanted there.
>>>>
>>>> http://en.wikipedia.org/wiki/Integer
>>
>> I already knew that, thanks.
>>
>> The practical implication however is that -1.0 < x < 1.0 can not be
>> *cast* to integers. At least not by the %d operator of string.format()
>
> There is no difference between 0 and 0.0 for Lua.
>
> --
> David Kastrup
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Bug in LNUM patch

Asko Kauppi

Fix to this problem was figured out and placed to my personal svn  
(where LNUM can be downloaded).

        svn co svn://slugak.dyndns.org/public/2010/LNUM2

However, I do not intend to make new "distribution" of this, nor carry  
on the LNUM patch to Lua 5.2.

-asko


Flemming Madsen kirjoitti 17.5.2010 kello 8:55:

> This is about Lua *with* the lnum patch (And I know, there's still no
> difference)
>
> This is all beside the point. The whole thing was brought to my
> attention by the fact that string.format("%d", 1.00001) was ok while
> string.format("%d", 0.999999) would give an error.
>
> This what the sample Lua session in my first post tried to show.
>
> Cheers /Flemming
>
>
> On Sun, May 16, 2010 at 11:43 AM, David Kastrup <[hidden email]> wrote:
>> Flemming Madsen <[hidden email]> writes:
>>
>>>> On Wed, May 12, 2010 at 4:58 AM, Richard Hundt <[hidden email]
>>>> > wrote:
>>>>
>>>>> Integers are defined as a subset of real numbers that are  
>>>>> *whole* (i.e which
>>>>> can be written without a fractional or decimal component).  
>>>>> Numbers between
>>>>> -1.0 < x < 1.0 therefore *cannot* be integers, so unless I've  
>>>>> misunderstood,
>>>>> the existing behaviour was exactly what you wanted there.
>>>>>
>>>>> http://en.wikipedia.org/wiki/Integer
>>>
>>> I already knew that, thanks.
>>>
>>> The practical implication however is that -1.0 < x < 1.0 can not be
>>> *cast* to integers. At least not by the %d operator of  
>>> string.format()
>>
>> There is no difference between 0 and 0.0 for Lua.
>>
>> --
>> David Kastrup
>>
>>