Rational

The Rational module provides exact fraction arithmetic. Unlike floating-point numbers, rationals maintain perfect precision for fractional values. Rationals are always stored in lowest terms (reduced form).

Creation

Rational.new
Int -> Int -> Option Rational
Creates a rational from numerator and denominator. Returns None if denominator is zero.
match Rational.new 3 4
  | Some r -> println (Rational.to-string r)
  | None -> println "Invalid"
# => "3/4"
Rational.from-int
Int -> Rational
Creates a rational from an integer (denominator = 1).
Rational.parse
String -> Option Rational
Parses a string like "3/4" or "5" into a rational.

Accessors

Rational.numerator
Rational -> Int
Returns the numerator of a rational.
Rational.denominator
Rational -> Int
Returns the denominator of a rational.

Arithmetic

Rational.add
Rational -> Rational -> Rational
Adds two rationals.
a = Rational.from-int 1 |> Option.unwrap
b = Rational.new 1 2 |> Option.unwrap
println (Rational.to-string (Rational.add a b))
# => "3/2"
Rational.subtract
Rational -> Rational -> Rational
Subtracts two rationals.
Rational.multiply
Rational -> Rational -> Rational
Multiplies two rationals.
Rational.divide
Rational -> Rational -> Option Rational
Divides two rationals. Returns None if dividing by zero.
Rational.negate
Rational -> Rational
Negates a rational (changes sign).
Rational.abs
Rational -> Rational
Returns the absolute value.
Rational.reciprocal
Rational -> Option Rational
Returns the reciprocal (1/x). Returns None for zero.
Rational.pow
Rational -> Int -> Option Rational
Raises a rational to an integer power.
Rational.add-int
Rational -> Int -> Rational
Adds an integer to a rational.
Rational.multiply-int
Rational -> Int -> Rational
Multiplies a rational by an integer.
Rational.mediant
Rational -> Rational -> Rational
Returns the mediant of two rationals ((a+c)/(b+d) for a/b and c/d).

Comparison

Rational.compare
Rational -> Rational -> Int
Returns -1 if less than, 0 if equal, 1 if greater than.
Rational.eq?
Rational -> Rational -> Bool
Tests equality.
Rational.ne?
Rational -> Rational -> Bool
Tests inequality.
Rational.lt?
Rational -> Rational -> Bool
Tests if less than.
Rational.le?
Rational -> Rational -> Bool
Tests if less than or equal.
Rational.gt?
Rational -> Rational -> Bool
Tests if greater than.
Rational.ge?
Rational -> Rational -> Bool
Tests if greater than or equal.

Predicates

Rational.zero?
Rational -> Bool
Returns true if the rational is zero.
Rational.one?
Rational -> Bool
Returns true if the rational is one.
Rational.positive?
Rational -> Bool
Returns true if positive.
Rational.negative?
Rational -> Bool
Returns true if negative.
Rational.integer?
Rational -> Bool
Returns true if the rational represents a whole number (denominator = 1).

Conversion

Rational.to-float
Rational -> Float
Converts to a floating-point number.
Rational.to-int
Rational -> Int
Truncates to an integer.
Rational.floor
Rational -> Int
Rounds down to the nearest integer.
Rational.ceil
Rational -> Int
Rounds up to the nearest integer.
Rational.round
Rational -> Int
Rounds to the nearest integer.
Rational.to-string
Rational -> String
Converts to a string like "3/4".
Rational.to-mixed
Rational -> String
Converts to a mixed number string like "1 1/2".
match Rational.new 7 4
  | Some r -> println (Rational.to-mixed r)
  | None -> println "Invalid"
# => "1 3/4"