dimanche 6 juillet 2014

Ruby 2.1 Fixnum missing one bit?


Vote count:

0




Ruby Fixnums are 64-bit on 64-bit rubies:



bits = 8 * 0.size
# => 64


The maximum signed integer under two's complement is 9_223_372_036_854_775_807, but the maximum Fixnum in Ruby is only half that:



[(1 << bits - 2) - 1, 1 << bits - 2].map(&:class)
# => [Fixnum, Bignum]
max = (1 << bits - 2) - 1
# => 4611686018427387903


How does Ruby use the missing bit? MRI Fixnum doc only mentions it briefly:



[Fixnum] Holds Integer values that can be represented in a native machine word (minus 1 bit). If any operation on a Fixnum exceeds this range, the value is automatically converted to a Bignum.



Tested with: MRI 2.1.2, JRuby 1.7.13, and Rubinius 2.2.10.



asked 1 min ago

glebm

6,391





Aucun commentaire:

Enregistrer un commentaire