module

Fractional

Fractional is a library that implements fractions and fractional arithmetic in Crystal beyond purely rational numbers (as is possible in the standard library with BigRational).

Currently, Fractional has implemented basic arithmetic with all base number types (i.e., those that inherit from Number (see /src/number/number.cr)).

two_over_three = frac(2, 3)
3 + two_over_three # => 11/3
two_over_three / 2 # => 2/6

Fractions are not automatically reduced to simplest terms, unlike BigRationals, so if your use case depends on keeping your values in simplest terms, account for this.

Constants

VERSION = "0.3.0"

Constructors

new(numerator, denominator) : Fractional::Fraction

Returns a new Fraction(T, U) with the given numerator and denominator.

NOTE: While it is a potential idea at the moment to potentially support non-Number and non-Fraction numerators and denominators (for example, for support of an algebraic mathematics library), that is currently not the main idea, and so the primary constructors (these and the frac macro) are intended to abstract the types away, and raise an exception when a bad type is supplied.

new(big_rational : BigRational) : Fractional::Fraction

Returns a new Fraction(T, U) with a numerator and denominator derived from the given big_rational.

new(float : Float) : Fractional::Fraction

Returns a new Fraction(T, U) with a numerator and denominator derived from a BigRational created by calling BigRational#new(float).

Class methods

additive_identity

Returns the additive identity of the type.

Nested types