1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

What's new in Ruby 2.1.0: Numerics

A Few Syntax Changes and New Bignum Library


This article is part of a series. To read more, see What's new in Ruby 2.1?.

Ruby has always had a diverse and flexible numerics system. You can simply using numbers and Ruby will figure the rest out. You can mix floating point and integers, you can do large calculations and Ruby will automatically detect the overflow and convert it to a bignum (a slower way of doing math, but arbitrarily precise). Ruby also has built in support for rational numbers (any number that can be represented as a ratio or fraction) and imaginary numbers.

Rational Literals

Rational numbers are any numbers that can be represented as a ratio or fraction of integers. These aren't used very often by most people, but they were very hard to stick into an equation before because of their very clunky syntax. A new literal syntax makes them very simple to use.

It's been a bit clunky to use rational numbers in the past. There was no real way to define a rational number without resorting to things like Rational(4,10). While not that ugly, it's a lot of noise for something that outside of Ruby is simply stated as 4/10 (which cannot be used in Ruby, as that would divide 4/10 using integer arithmetic, which just produces 0). Now, there are numeric literals that can be used for rationals using the r suffix. You can now say 4.10r to represent 4/10 as a Rational object. You can also leave off the decimal point for any rational over 1, for example 10r is the same as 10.1r. Using statements like 4.10r * 3, Ruby now produces a new Rational object with the value of 6.5r (the same thing as 12.10r). While this is not the traditional rational syntax, this is a simple and effective compromise to make Rational numbers a first class Ruby type and encourage people to use it.

Imaginary Literals

Imaginary numbers are used to solve equations that involve more than one solution (such as the quadratic equation). They're not often used in your day to day Ruby scripts and web applications, but they do have their uses in some areas (especially electrical engineering, where imaginary numbers are used to calculate the phase of an AC signal) and are a welcome addition to the Ruby landscape. While Ruby has had imaginary numbers for a while, a small change has made them slightly easier to use.

Imaginary numbers were made a bit easier to use as well. While in the past you could say 3+4.i using the Fixnum#i method, which is only one character away from the traditional mathematical representation of imaginary numbers. However, as long as they're adding suffixes for numerics, why not add one for imaginary numbers? The new suffix i does just what you'd expected, allow you to say 3+4i to represent the imaginary component of an imaginary number.

Bignum is Faster

If you use Bignum, performance is always an issue and there's not much you can do about it. Bignum is just slow compared to native arithmetic. However, on systems that can handle 128 bit integers (which includes x86, the system you're likely working with) Bignum can be quite a bit faster now. Bignum also gets a boost from the GNU Multiple Precision Bignum Library. So if you're using bignum, expect faster math.

  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. New Features in Ruby 2.1.0
  6. What's new in Ruby 2.1.0: Numerics

©2014 About.com. All rights reserved.