Ruby 2.4 will be merging both
Bignum into the same class (
Integer) so I think this is a good time to review the different number types in Ruby!
And that’s what we are going to talk about in this post 🙂
An Overview of Number Types
Let’s start by taking a look at the class hierarchy of all the number related classes in Ruby:
BigDecimal (Standard Library)
As you can see, the
Numeric class is the parent for all the number classes. Remember that you can use the
ancestors method to discover the parent classes for any class.
Fixnum.ancestors - Fixnum.included_modules
[Fixnum, Integer, Numeric, Object, BasicObject]
Now let’s see these classes in table form:
||Normal numbers that fit into the OS integer type
||Used for bigger numbers
||Imprecise decimal numbers
||Used for math stuff with imaginary numbers
||Used to represent fractions
||Perfect precision decimal numbers
You may have noticed that in the description for the
Float class it says “imprecise”, what’s the meaning of that?
Let me show you with an example:
Why is this false? Let’s look at the result of
0.2 + 0.1.
And that’s what I mean by imprecision! The reason this happens is because of the way that a float is stored. If you need decimal numbers that are always accurate you can use the
BigDecimal("0.2") + BigDecimal("0.1") == 0.3
Why don’t we always use
BigDecimal then? Because it’s a lot slower!
Here is a benchmark:
bigdecimal 21.559k i/100ms
float 79.336k i/100ms
bigdecimal 311.721k (± 7.4%) i/s - 1.552M
float 3.817M (±11.7%) i/s - 18.803M
float: 3817207.2 i/s
bigdecimal: 311721.2 i/s - 12.25x slower
BigDecimal is 12 times slower than
Float, and that’s why it’s not the default 🙂
Fixnum vs Bignum
In this section I want to explore the differences between
Let’s start with some code:
Ruby creates the correct class for us, and it will automatically promote a
Fixnum to a
Bignum when necessary.
Note: You may need a bigger number to get a
Bignum object if you have a 64-bit Ruby interpreter.
Why do we need different classes? The answer is that to work with bigger numbers you need a different implementation, and working with big numbers is slower, so we end up with a similar situation to
Fixnum class also has some special properties. For example, the object id is calculated using a formula.
The formula is:
(number * 2) + 1.
But there is more to this, when you use a
Fixnum there is no object being created at all. There is no data to store in a
Fixnum, because the value is derived from the object id itself. This is just an implementation detail, but I think it’s interesting to know 🙂
MRI (Matz’s Ruby Interpreter) uses these two macros to convert between value & object id:
INT2FIX(i) ((VALUE)(((SIGNED_VALUE)(i))<<1 | FIXNUM_FLAG))
What happens here is called “bit shifting”, which moves all the bits to the left or the right. Shifting one position to the left is equivalent to multiplying by 2 & that’s why the formula is
(number * 2) + 1. The +1 comes from the
Bignum works more like a normal class & uses normal object ids:
All this means is that
Fixnum objects are closer to symbols in terms of how they work at the interpreter level, while
Bignum objects are closer to strings.
In this post you learned about the different number-related classes that exist in Ruby.
You learned that floats are imprecise, and that you can use
BigDecimal if accuracy is a lot more important than performance. And after that you learned that
Fixnum objects are special at the interpreter level, but
Bignums are just regular objects.
If you found this post interesting don’t forget to sign-up to my newsletter in the form below 🙂
Thanks for the intro to numbers in ruby! How did you produce the benchmark output?