biggest number

J

Javier Esteve

I'm learning ruby, and I want to know if it is any way to know the
biggest number to be represented with a specific type of number (Fixnum,
Bignum, Float).

Thank you.
 
C

Charles Oliver Nutter

Javier said:
I'm learning ruby, and I want to know if it is any way to know the
biggest number to be represented with a specific type of number (Fixnum,
Bignum, Float).

Float is a standard 64-bit IEEE floating point value. There are several
constants on the Float class that can tell you whatever you need:

âž” jruby -e "p Float::constants"
["MAX", "MAX_10_EXP", "MANT_DIG", "RADIX", "DIG", "MIN", "ROUNDS",
"MIN_EXP", "EPSILON", "MIN_10_EXP", "MAX_EXP"]

Fixnum has no such constants, but on most implementations (CRuby, JRuby,
Rubinius at least) its max size is dependent on how large its basis
representation is. I believe it can be either "32" or "64" bits on
CRuby, where on JRuby it is always "64" bits:

âž” ruby -e "p (1 << (1.size * 8 - 1)) - 1"
2147483647

âž” ruby -e "p -(1 << (1.size * 8 - 1))"
-2147483648

âž” jruby -e "p (1 << (1.size * 8 - 1)) - 1"
9223372036854775807

âž” jruby -e "p -(1 << (1.size * 8 - 1))"
-9223372036854775808

The loss of one bit of precision on most impls is to emulate CRuby's
implementation of Fixnum as a tagged pointer, where the top bit
indicates whether an object pointer is a real pointer or a Fixnum value).

And Bignum has no min or max size, other than the maximum size of a
byte[] on your system.

- Charlie
 
J

Joel VanderWerf

Javier said:
I'm learning ruby, and I want to know if it is any way to know the
biggest number to be represented with a specific type of number (Fixnum,
Bignum, Float).

Thank you.

irb(main):005:0> (2**30).class
=> Bignum
irb(main):006:0> (2**30-1).class
=> Fixnum

irb(main):009:0> RUBY_VERSION
=> "1.8.6"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top