Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max and Min value of Fixnum and Bignum in Ruby

Tags:

ruby

I'm wondering the range of Fixnum and Bignum. In Float, I can get the range of Float with Float::MAX and Float::MIN. There are not analogous constants in Fixnum and Bignum. What's the best way to achieve this?

like image 714
niaomingjian Avatar asked Oct 14 '16 03:10

niaomingjian


1 Answers

Forget Fixnum and Bignum. They are private internal implementation details. They are optimizations. You should ignore them. The only class you should be concerned about is Integer.

The ISO Ruby Language Specification only specifies the Integer class. It allows for implementation-specific subclasses, but it doesn't specify them.

Fixnums are an optimization that should be internal to the implementation and should never have been exposed to the programmer. Think about flonums in YARV. Did you even notice flonums were introduced in YARV 2.0? All they do is make certain Floats faster. But you never see them. Which is as it should be.

And in fact, YARV 2.4 removes Fixnum and Bignum and will only have Integers in the future. Fixnum and Bignum will stay around for a while as aliases for Integer, for backwards-compatibility reasons, but the classes themselves will be gone.

In Ruby, Integers don't have a maximum or minimum value; they are arbitrary-size integers. Therefore, there aren't any such constants.

The size limit for Fixnums (if they exist at all) depends on the platform and implementation. On JRuby, Fixnums are always 64-bit, on YARV, they are 63-bit on 64-bit platforms and 31-bit on 32-bit platforms. On Opal, Integers are implemented as ECMAScript Numbers (which are actually IEEE754 floats and thus are only accurate for 53-bits), and I don't think they even have Fixnums or Bignums.

like image 55
Jörg W Mittag Avatar answered Sep 28 '22 08:09

Jörg W Mittag