#### Topic: Data type

Question

I have been searching through the internet to find out how many bytes are contained in

a ruby integer and a ruby double?

viper20164

**This is an archive from Rails Forum.** Check out the new community here!

Pages **1**

Question

I have been searching through the internet to find out how many bytes are contained in

a ruby integer and a ruby double?

viper20164

Integer is the basis for the two concrete classes that hold whole numbers, Bignum and Fixnum.

32 bit system max fixnum should be (2**31 - 1).

64 bit system max fixnum is (2**62 - 1).

So fixnum is 4 or 8 bytes.

If you got greater number than that, then integer automatically uses Bignum class. I'm not sure if there are any certain limits.

*Last edited by innu (2009-05-05 18:12:41)*

Float max is for me (64bit system(not sure if that matters here))

Float::MAX

=> 1.79769313486232e+308

this is almost 2**1024.

innu wrote:

Integer is the basis for the two concrete classes that hold whole numbers, Bignum and Fixnum.

32 bit system max fixnum should be (2**31 - 1).

64 bit system max fixnum is (2**62 - 1).So fixnum is 4 or 8 bytes.

If you got greater number than that, then integer automatically uses Bignum class. I'm not sure if there are any certain limits.

I'm no expert on numbers but shouldn't it be 2**63-1 on a 64 bit system? If not why not.

Actually you should be right. But I don't know why it shows Bignum for me then.

irb(main)> (2**63 - 1).class

=> Bignum

irb(main)> (2**62 - 1).class

=> Fixnum

EDIT:

I tried with my 32 bit machine

irb(main)> (2**31 - 1).class

=> Bignum

irb(main)> (2**30 - 1).class

=> Fixnum

Hmm.. I need to do a little research about this.

*Last edited by innu (2009-05-06 19:51:41)*

In the Ruby docs it says:

A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit).

So you only get 31 bits for the value, and out of those 31, 1 bit is reserved for the sign, leaving only 30 bits for the *actual* value.

Why not 32 bits for the value? Because Ruby uses a bit to handle some of its internals (oo-err ):

http://www.oreillynet.com/ruby/blog/200 … lue_1.html

So it's:

- 1 bit for Ruby to do its thing

- 1 bit for sign

- 30/62 bits for number

Rob Anderton

TheWebFellas

TheWebFellas

Pages **1**

Hosting provided by aTech Media