[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: lua_number2int
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Tue, 14 Nov 2006 15:35:28 -0500
Here I get this:
% gcc --version
gcc (GCC) 3.4.4 (mingw special)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% cat test.c
#include <stdio.h>
int main()
{
printf("0x%08x\n", (int)(double)0xffffffff);
printf("0x%08x\n", (int)(unsigned int)(double)0xffffffff);
printf("%d\n", (int)(double)-4.0);
printf("%d\n", (int)(unsigned int)(double)-4.0);
return 0;
}
% make test
% test
0x7fffffff
0xffffffff
-4
0
As you can see with your additionnal unsigned int cast you lose all negative values. That's because (unsigned int)0xffffffff and (int)0xffffffff have two different representations in double precision floating point format.
> -----Message d'origine-----
> De : lua-bounces@bazar2.conectiva.com.br
> [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de
> askok@dnainternet.net
> Envoyé : 14 novembre 2006 07:21
> À : Lua list
> Objet : Re: lua_number2int
>
>
> There may be several abnormal things regarding the normal Lua
> integer handling.
>
> What I find most weird (met in making the "integer
> patches") is that floating point values are silently
> converted into integers by 'lua_tointeger()'. I would
> consider this a BUG, since if a C routine specifically
> requests an integer I think it should be provided one.
> Anyways, that behaviour remains in the "integer patch" so
> that outside behaviour remains unaltered.
>
> Could we have this changed, in say 5.2?
>
> Also "lua_tounsigned()" would be handy.
>
> -asko
>
>
> On Mon, 13 Nov 2006 21:52:56 -0500
> "Brian Weed" <bw@imaginengine.com> wrote:
> > Today, we converted a project from Lua v5.02 to v5.1.1, and
> we started
> >having a problem when converting double to int.
> > I have narrowed my problem down to code that has nothing to do with
> >Lua. But I still wanted to get other people's opinions about it.
> >
> > double d = 0xFFFFFFFF;
> > int n;
> > n = (int)d;
> >
> > // n now contains 0x80000000
> >
> > n = (int)(unsigned int)d;
> >
> > // n now contains 0xFFFFFFFF
> >
> > (this happens on both VS2005, and GCC 4.0.1 (Apple Computer, Inc.
> >build 5363))
> >
> > Since lua_number2int and lua_number2integer both basically
> do this:
> >"n = (int)d;"
> >
> > Wouldn't it be safer to do this: "n = (int)(unsigned
> int)d;" so as to
> >preserve the values that would otherwise be truncated ?
> >
> > What sort of problems would this cause? Is my fix unportable?
> >
> > The initial problem we got stems from doing this in Lua:
> >
> > d = 0xFFFFFFFF
> >Foo(d)
> >
> > // Assuming Foo() is something like this...
> >
> > int bind_Foo(lua_State *L)
> > {
> > Foo(lua_tointeger(L, 1)); // value passed to Foo()
> >is now
> > truncated to 0x80000000
> >
> > return(0);
> > }
> >
> > Brian
> >
> >
> >
> >
>
>