From: David Masover Date: 2009-09-15T10:08:33+09:00 Subject: Re: Overflow behavior On Sunday 13 September 2009 05:52:03 pm Joel VanderWerf wrote: > jacob navia wrote: > > What is the official behavior of ruby in case of integer overflow? > > Integers overflow from Fixnums (30 bits) into Bignums (unlimited > precision integers). Not entirely accurate, apparently: irb(main):009:0> (2**62-1).class => Fixnum irb(main):010:0> (2**62).class => Bignum irb(main):012:0> ((-2)**62-1).class => Fixnum irb(main):013:0> ((-2)**62).class => Bignum Fixnum seems to be a signed 63-bit integer (62 bits plus the sign bit) on my 64-bit system. I'm guessing that it's actually a 31-bit signed integer on a 32-bit system. This is nice in that my programs don't really care, but will use a 64-bit integer where it's efficient -- yet it strikes me that for smaller numbers, 32 bits would be more efficient, RAM-wise, if nothing else. Then again, I don't know from this (nor should I have to care) how many bits Fixnum actually uses. Other interesting things: There only seems to be one Float class, and it can't go as high as Bignum. irb(main):033:0> (2**1023).to_f => 8.98846567431158e+307 irb(main):034:0> 2**1024 => 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216 irb(main):035:0> (2**1024).to_f => Infinity And there's your answer for overflow behavior. It seems Bignum integers will happily consume all your RAM if you let them, whereas Floats will occasionally round up to Infinity.