struct

Fractional::Fraction(T, U)

Inherits Comparable < Comparable < Comparable < Comparable < Fractional::Arithmetic < Struct < Value < Object

This is the star of the show, as it were.

A Fraction is really a container of (technically) any two values of any two types. This framework was chosen to allow for the ability to represent different values for any given numer-/denominator, including other fractions. This would not be possible without an extremly complicated inheritance system otherwise, since all structs ultimately inherit from the same memory address, meaning that a struct would refer to itself, causing lots of recursion issues.

This setup ensures that, no matter how deep you decide to nest a fraction, it won't be a nightmare to fight with typing.

Constructors

new(numerator : T, denominator : U)

The "true" constructor for the type; returns a new Fraction(T, U) with the given numerator and denominator

DEPRECATED: this is not intended to be called directly by the end-user. End-users should use Fractional.new or frac(numerator, denominator) for creating regular fractions.

Instance methods

*(other : Fraction) : Fraction

Returns the result of multiplying self and other as fractions.

*(other : Int) : Fraction

Returns the result of multiplying a fraction by an integer.

*(other : Float) : Fraction

Returns the product of self and other cast to Fraction.

**(other : Int) : Fraction

Returns a fraction whose numerator and denominator are the result of exponentiating self's numerator and denominator.

+(other : Fraction) : Fraction

Returns the result of adding self and other as fractions.

+(other : Int) : Fraction

Returns the result of adding a fraction to an integer.

+(other : Float) : Fraction

Returns the sum of self and other cast to Fraction.

-(other : Fraction) : Fraction

Returns the result of subtracting self and other as fractions.

-(other : Int) : Fraction

Returns the result of subtracting a fraction from an integer.

-(other : Float) : Fraction

Returns the difference of self and other cast to Fraction.

/(other : Fraction) : Fraction

Returns the result of dividing self and other as fractions (so really, multiplying by the reciprocal).

/(other : Int) : Fraction

Returns the result of dividing a fraction with an integer (so really, multiplying by 1 over the integer)

/(other : Float) : Fraction

Returns the quotient of self and other cast to Fraction.

<=>(other : BigRational) : Int32 | Nil

Compares self with other of type BigRational.

Returns -1 if self is less than other, 1 if self is greater than other, 0 if self is the same as other, and nil otherwise.

<=>(other : Int) : Int32 | Nil

Compares self with other of type Int.

Returns -1 if self is less than other, 1 if self is greater than other, 0 if self is the same as other, and nil otherwise.

<=>(other : Float) : Int32 | Nil

Compares self with other of type Float.

Returns -1 if self is less than other, 1 if self is greater than other, 0 if self is the same as other, and nil otherwise.

<=>(other : Fraction) : Int32 | Nil

Compares self with other of type Fraction.

Returns -1 if self is less than other, 1 if self is greater than other, 0 if self is the same as other, and nil otherwise.

denominator
numerator
reciprocal

Returns the reciprocal of self.

reduce

Returns the self in simplest terms.

sign

Returns self's sign as an Int32

to_f

Returns the value of the numerator divided by the denominator as a Float64.

to_f32

Returns the value of the numerator divided by the denominator as a Float32.

to_f64

Returns the value of the numerator divided by the denominator as a Float64.

to_i?

If self can be reduced to simplest terms with a denominator of 1, return that fraction's numerator. Otherwise, return nil.

to_s(io : IO) : Nil

Writes a String representation of self to io.

to_s

Returns a String representation of self.