[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: lua_number2int
- From: David Given <dg@...>
- Date: Tue, 14 Nov 2006 10:58:02 +0000
Brian Weed 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
On my system (Ubuntu Edgy Linux, gcc 4.1.2) n now containts 0x7FFFFFFF, which
is what I would consider to be correct.
> n = (int)(unsigned int)d;
> // n now contains 0xFFFFFFFF
I get this too. I also consider this to be correct, because casting d to an
unsigned int will fit, and will therefore not truncate the value; and casting
an unsigned int to an int doesn't truncate (which I've never really understood).
╭─┈David Given┈──McQ─╮ "A line dancer near a graduated cylinder, the
│┈ email@example.com┈┈┈┈│ blithe spirit inside some wheelbarrow, and a tomato
│┈(firstname.lastname@example.org)┈│ are what made America great!" --- received via spam
Description: OpenPGP digital signature