# Bignum-Fixnum-Numeric confusion

**URL:** <https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174>\
**Category:** Ruby\
**Created:** [March 6, 2013, 12:52pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174 "2013-03-06T12:52:09Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 12:52pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/1 "2013-03-06T12:52:09Z")

</div>

Hi,

I am newbie to Ruby.

But couldn’t properly understand which rules in Ruby decides the below  
object’s when any number comes to it?

`Bignum` `Fixnum` `Numeric` `Integer`.

I am basically from `C` background. So these many classification making  
me confused.

Sorry for my English, It’s horrible I know 🙂

Thanks in advance for your help!  
Pritam

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 1:10pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/2 "2013-03-06T13:10:55Z")

</div>

Say you’re on a 64-bit system. In that case, any numbers with absolute  
value less than 2^62 will be a Fixnum:

> > (2\*\*62 - 1).class  
> > =\> Fixnum

Fixnums are usually described as “immediate” values. In MRI at least,  
their value is encoded in their object ID using 2n+1:

> > (-2..2).map { |e| [e, e.object\_id] }  
> > =\> [[-2, -3], [-1, -1], [0, 1], [1, 3], [2, 5]]

Anything greater than or equal to 2^62 in absolute value will come out  
as a Bignum, which are “normal” instances of the Bignum class.

> > (2\*\*62).class  
> > =\> Bignum

There’s no such object ID stunt here:

> > (0..4).map { |e| [(2**62 + e), (2**62 + e).object\_id] }  
> > =\> [[4611686018427387904, 70120613511800], [4611686018427387905,  
> > 70120613511580], [4611686018427387906, 70120613511360],  
> > [4611686018427387907, 70120613511140], [4611686018427387908,  
> > 70120613510920]]

(For n-bit systems, Fixnum values are stored with n-1 bits since 1 bit  
is used by the VM, so the range of values for a Fixnum is (-2^(n-1) +  
1, 2^(n-1) - 1). Not sure if this is exactly the same with other Ruby  
implementations.)

Integer is the superclass of both, for cases where you wish to simply  
reason about integers:

> > Bignum.ancestors  
> > =\> [Bignum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]  
> > Fixnum.ancestors  
> > =\> [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]

Numeric is to cover Integers and other types of number:

> > Float.ancestors  
> > =\> [Float, Numeric, Comparable, Object, Kernel, BasicObject]

For example, Complex in the stdlib mathn library is a subclass of  
Numeric:

> > require “mathn”  
> > =\> true  
> > Complex(2, 5)  
> > =\> (2+5i)  
> > Complex.ancestors  
> > =\> [Complex, Numeric, Comparable, Object, Kernel, BasicObject]

Hope that helps!

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 1:12pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/3 "2013-03-06T13:12:24Z")

</div>

Numeric General Class for all of them

- Integer: your int type in C (base class for the other two)
  - FixNum : they are stored as bitshift, so 2.object\_id ==  
2.object\_id
  - BigNummer to big to store as bitshift, so they are real objects  
with different object\_ids

Fixnum: (10**18).object\_id == (10**18).object\_id #=\> true  
Bignum: (10**19).object\_id == (10**19).object\_id #=\> false

Float, Complex and Rational are other Numeric classes

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 1:37pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/4 "2013-03-06T13:37:13Z")

</div>

@Hans thanks for your care.

Please find the below I am getting something different in my IRB:

I am using the below Ruby version.

ruby 1.9.3p374 (2013-01-15) [i386-mingw32]

> > (10**18).object\_id == (10**18).object\_id  
> > =\> false  
> > (10**19).object\_id == (10**19).object\_id  
> > =\> false

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 1:56pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/5 "2013-03-06T13:56:32Z")

</div>

You’re on a 32 bit system so your cutoff is lower. Use 2\*\*29 for  
example.

You can see the size of a fixnum with 0.size

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 1:48pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/6 "2013-03-06T13:48:51Z")

</div>

its because of your 32 bit architecture

so i think  
(10**9).object\_id == (10**9).object\_id #returns true  
(10**10).object\_id == (10**10).object\_id #returns false

for you?

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 2:13pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/7 "2013-03-06T14:13:53Z")

</div>

© because in C an Fixnum is only an bitshift …  
other ruby objects are struct RObject objects … in this RObject  
there are flags and options and entries for instance variables and  
singleton methods

so there is no space where this stuff could be defined in a Fixnum

(in 1.9 it was possible to define instance variables into Fixnums but  
this was dropped in 2.0)

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 2:00pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/8 "2013-03-06T14:00:16Z")

</div>

From the doc: [Class: Fixnum (Ruby 1.9.3)](http://ruby-doc.org/core-1.9.3/Fixnum.html)

I found the below features :

Fixnum:

(a) Fixnum objects have immediate value. This means that when they are  
assigned or passed as parameters, the actual object is passed, rather  
than a reference to that object. - Can the same be shown in IRB. Hope  
then only it will be correctly understood by me.

(b) Assignment does not alias Fixnum objects. - What does it actually  
then?

(c) “There is effectively only one Fixnum object instance for any given  
integer value, so, for example, you cannot add a singleton method to a  
`Fixnum`.” - Couldn’t understand the reason of not to add the  
`singleton` method with Fixnum object instances.

– the above (b) and (c) points are also made me confused.

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 2:50pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/9 "2013-03-06T14:50:54Z")

</div>

On Wed, Mar 6, 2013 at 2:00 PM, Pritam D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> From the doc: [Class: Fixnum (Ruby 1.9.3)](http://ruby-doc.org/core-1.9.3/Fixnum.html)
> 
> I found the below features :
> 
> Fixnum:
> 
> (a) Fixnum objects have immediate value. This means that when they are  
> assigned or passed as parameters, the actual object is passed, rather  
> than a reference to that object. - Can the same be shown in IRB. Hope  
> then only it will be correctly understood by me.

The fact that Fixnums are “immediate values” is just an implementation  
detail. From the perspective of a user of the language it does not  
make a functional difference (this is about performance only). I  
suggest you take a different route and start with basic understanding  
of how numeric classes and general OO and variables work in Ruby.

> (b) Assignment does not alias Fixnum objects. - What does it actually  
> then?

From a logical point of view it does:

irb(main):001:0\> a = 1  
=\> 1  
irb(main):002:0\> a.class  
=\> Fixnum  
irb(main):003:0\> b = a  
=\> 1  
irb(main):004:0\> b.class  
=\> Fixnum  
irb(main):005:0\> a == b  
=\> true  
irb(main):006:0\> a.equal? b  
=\> true  
irb(main):007:0\> a.object\_id == b.object\_id  
=\> true

> (c) “There is effectively only one Fixnum object instance for any given  
> integer value, so, for example, you cannot add a singleton method to a  
> `Fixnum`.” - Couldn’t understand the reason of not to add the  
> `singleton` method with Fixnum object instances.

I think the reasoning is flawed: the fact that there is only one  
instance of every Fixnum does by no means prevent associating  
singleton methods with the instance. For example, it’s still possible  
to store instance variables in a Fixnum instance:

# same a and b as above

irb(main):009:0\> a.instance\_variable\_set ‘@x’, “yeah!”  
=\> “yeah!”  
irb(main):010:0\> a.instance\_variables  
=\> [:@x]  
irb(main):011:0\> a.instance\_variable\_get ‘@x’  
=\> “yeah!”  
irb(main):012:0\> b.instance\_variables  
=\> [:@x]  
irb(main):013:0\> b.instance\_variable\_get ‘@x’  
=\> “yeah!”  
irb(main):014:0\> 1.instance\_variable\_get ‘@x’  
=\> “yeah!”  
irb(main):015:0\> 2.instance\_variable\_get ‘@x’  
=\> nil

It’s just an implementation decision to not allow singleton methods for  
Fixnum.

> – the above (b) and (c) points are also made me confused.

As I said above. For understanding when Ruby will switch between  
types (your original question, as I understand it) this is irrelevant.  
If you want to understand how that works (method #coerce is central  
to that) you can read my blog post which describes how to create a  
class which adheres to the conventions of operator overloading and  
doing math in Ruby:  
[http://blog.rubybestpractices.com/posts/rklemme/019-Complete\_Numeric\_Class.html](http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 4:27pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/10 "2013-03-06T16:27:55Z")

</div>

On Wed, Mar 6, 2013 at 3:02 PM, Hans M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > to store instance variables in a Fixnum instance:
> 
> i need to resay that this is not possible in 2.0 anymore … so you dont  
> should depend on it

I didn’t say it’s a good idea. 🙂

Cheers

robert

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 4:37pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/11 "2013-03-06T16:37:30Z")

</div>

Great Thanks to you guys:

Just need to stuck myself from two more areas.

From the same doc:

(a) While Fixnum values are immediate, Bignum objects are not —  
assignment and parameter passing work with references to objects, not  
the objects themselves. - Actually as far as I know we are passing the  
references to the objects,not the actual objects. But here I couldn’t  
follow the taste.

(b)When a calculation involving Bignum objects returns a result that  
will fit in a Fixnum, the result is automatically converted. - In `C` I  
found some rules such automatic conversions. But does the same  
applicable here also or other rules defined here specially for Ruby?

©For the purposes of the bitwise operations and [], a Bignum is  
treated as if it were an infinite-length bitstring with 2’s complement  
representation. - Why so?

Thanks for all of your collaborative help to understand this nice  
concept in Ruby.

Thanks,  
Pritam

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 3:02pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/12 "2013-03-06T15:02:18Z")

</div>

Robert K. wrote in post #1100376:

> > (c) “There is effectively only one Fixnum object instance for any given  
> > integer value, so, for example, you cannot add a singleton method to a  
> > `Fixnum`.” - Couldn’t understand the reason of not to add the  
> > `singleton` method with Fixnum object instances.
> 
> I think the reasoning is flawed: the fact that there is only one  
> instance of every Fixnum does by no means prevent associating  
> singleton methods with the instance. For example, it’s still possible  
> to store instance variables in a Fixnum instance:

i need to resay that this is not possible in 2.0 anymore … so you dont  
should depend on it

---

<div class="post-metadata">

**Author:** ![pritamdey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pritamdey](https://www.ruby-forum.com/u/pritamdey)\
**Post date:** [March 6, 2013, 5:24pm UTC](https://www.ruby-forum.com/t/bignum-fixnum-numeric-confusion/227174/13 "2013-03-06T17:24:37Z")

</div>

On Wed, Mar 6, 2013 at 4:37 PM, Pritam D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> follow the taste.  
> See earlier

> (b)When a calculation involving Bignum objects returns a result that  
> will fit in a Fixnum, the result is automatically converted. - In `C` I  
> found some rules such automatic conversions. But does the same  
> applicable here also or other rules defined here specially for Ruby?

See ealier

> (c)For the purposes of the bitwise operations and , a Bignum is  
> treated as if it were an infinite-length bitstring with 2s complement  
> representation. - Why so?

Because it’s the most reasonable thing to do when dealing with binary  
numbers of arbitrary number of bits.

Cheers

robert
