5.12. Using mathn

For math-intensive programs, you will want to know about the excellent mathn library created by Keiju Ishitsuka. It provides a few convenience methods and classes, and in general helps to unify all of Ruby’s numeric classes so that they “play well” together.

The easiest way to “use” this library is simply to require it and forget it. Because it requires the complex, rational, and matrix libraries (in that order), there is no need to do separate requires of those if you are using them.

In general, the mathn library tries to produce “sensible” results from computations—for example, the square root of a Rational will be returned when possible as another Rational rather than a Float. Table 5.1 shows some typical effects of loading ...

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.