Projects

Ticket #748 (new defect)

Opened 20 months ago

Last modified 14 months ago

32bit Number Limit and Conversion

Reported by: lp@… Owned by: lsansonetti@…
Priority: major Milestone: MacRuby Later
Component: MacRuby Keywords: 32bit number
Cc:

Description (last modified by martinlagardette@…) (diff)

there seems to be a bug in the 32bit release of 0.6 with numbers

arch -arch i386 macirb --simple-prompt
>> 0x44485250                         # reality check
=> 1145590352
>> NSNumber.numberWithInt(0x44485250) # then see all 32 NSNumber are weird...
=> 71848528
>> NSNumber.numberWithFloat(0x44485250)
=> 1145590272.0
>> NSNumber.numberWithInt(1145590352)
=> 71848528
>> NSNumber.numberWithFloat(1145590352)
=> 1145590272.0

versus:

arch -arch x86_64 macirb --simple-prompt
>> 0x44485250                             # reality check
=> 1145590352
>> NSNumber.numberWithInt(0x44485250)
=> 1145590352
>> NSNumber.numberWithFloat(0x44485250)
=> 1145590400.0
>> NSNumber.numberWithInt(1145590352)
=> 1145590352
>> NSNumber.numberWithFloat(1145590352.0) # this is weird again...
=> 1145590400.0

to look a bit further, the bug turning point seems to be here for the 32bit release:

>> NSNumber.numberWithInt(536870910)
=> 536870910
>> NSNumber.numberWithInt(536870911)
=> 536870911
>> NSNumber.numberWithInt(536870912)
=> -536870912
>> NSNumber.numberWithInt(536870913)
=> -536870911

and here in the 64 bit:

>> NSNumber.numberWithInt(2147483646)
=> 2147483646
>> NSNumber.numberWithInt(2147483647)
=> 2147483647
>> NSNumber.numberWithInt(2147483648)
=> -2147483648
>> NSNumber.numberWithInt(2147483649)
=> -2147483647

also an other detail:

32bit turning point:
>> 536870911 / 16
=> 33554431

64bit turning point:
>> 2147483647 / 64
=> 33554431

Change History

Changed 20 months ago by martinlagardette@…

  • description modified (diff)

Changed 14 months ago by isaackearse@…

I can reproduce this issue on macruby trunk r4979

Changed 14 months ago by lsansonetti@…

  • milestone changed from MacRuby 0.7 to MacRuby Later

I think MacRuby behaves correctly here. When running in 32-bit mode, the fixnum range is much smaller, so 0x44485250 is actually a Bignum. Passing 0x44485250 to the +[NSNumber numberWithInt:] will get its value coerced to a regular C integer.

I am not sure what MacRuby should do differently here. Maybe raising an exception instead of coercing the object?

Screening to Later in the meantime.

Note: See TracTickets for help on using tickets.