v2026.11.2
All Bundles
Bundle

Arbitrary-precision and exact arithmetic.

Objeck's `Int` is 64-bit and wraps silently rather than trapping, so a calculation that exceeds it produces a plausible wrong answer with no diagnostic. `BigInt` is exact at any size instead, bounded only by memory. `Rational` is an exact fraction on `BigInt`, and `Complex` a complex number with `Float` parts.

Compile with `-lib math`.

Rational

An exact fraction, backed by `BigInt` so no operation can overflow.

Always in lowest terms with a positive denominator, so equal values are identical and `Compare` is a comparison of two products. Immutable: every operation returns a new value.

Implements: Compare, Stringify

Example

# the harmonic number H(5), exactly rather than as a float
h := Rational->Of(0);
each(i : 5) { h := h->Add(Rational->Of(1, i + 1)); };
h->ToString()->PrintLine();     # 137/60

Operations

Abs #

The absolute value.

method : public : Abs() ~ Rational

Return

TypeDescription
Rationalthe magnitude

Add #

The sum, reduced to lowest terms.

method : public : Add(rhs:Rational) ~ Rational

Parameters

NameTypeDescription
rhsRationalvalue to add

Return

TypeDescription
Rationalthe sum, or Nil if rhs is Nil

Add #

The sum with a whole number.

method : public : Add(whole:Int) ~ Rational

Parameters

NameTypeDescription
wholeIntvalue to add

Return

TypeDescription
Rationalthe sum

Ceiling #

The least integer not less than this value.

method : public : Ceiling() ~ BigInt

Return

TypeDescription
BigIntceiling

Compare #

-1, 0 or 1 as this value is less than, equal to or greater than the argument. Both denominators are positive, so comparing the cross products keeps the inequality's direction.

method : public : Compare(rhs:Compare) ~ Int

Parameters

NameTypeDescription
rhsCompareother value

Return

TypeDescription
Intordering

Divide #

Division. Dividing by zero yields Nil.

method : public : Divide(rhs:Rational) ~ Rational

Parameters

NameTypeDescription
rhsRationaldivisor

Return

TypeDescription
Rationalquotient

Divide #

Division by a whole number.

method : public : Divide(whole:Int) ~ Rational

Parameters

NameTypeDescription
wholeIntdivisor

Return

TypeDescription
Rationalthe quotient, or Nil if whole is zero

Equals #

Whether two values are equal. Lowest terms are unique, so this is a comparison of numerators and denominators.

method : public : Equals(rhs:Rational) ~ Bool

Parameters

NameTypeDescription
rhsRationalvalue to compare against

Return

TypeDescription
Booltrue if equal

Floor #

The greatest integer not greater than this value.

BigInt division truncates toward zero, which is the floor only for non-negative values; a negative value with a remainder steps down.

method : public : Floor() ~ BigInt

Return

TypeDescription
BigIntfloor

GetDenominator #

The denominator, in lowest terms and always positive.

method : public : GetDenominator() ~ BigInt

Return

TypeDescription
BigIntdenominator

GetNumerator #

The numerator, in lowest terms, carrying the sign.

method : public : GetNumerator() ~ BigInt

Return

TypeDescription
BigIntnumerator

GreaterEqual #

Whether this value is greater than or equal to the argument.

method : public : GreaterEqual(rhs:Rational) ~ Bool

Parameters

NameTypeDescription
rhsRationalvalue to compare against

Return

TypeDescription
Booltrue if greater or equal

GreaterThan #

Whether this value is greater than the argument.

method : public : GreaterThan(rhs:Rational) ~ Bool

Parameters

NameTypeDescription
rhsRationalvalue to compare against

Return

TypeDescription
Booltrue if greater

HashID #

A hash consistent with Compare: lowest terms are unique, so equal values have identical numerators and denominators.

method : public : HashID() ~ Int

Return

TypeDescription
Inthash

IntegerDivide #

Flooring integer division: the quotient rounded toward negative infinity.

method : public : IntegerDivide(rhs:Rational) ~ BigInt

Parameters

NameTypeDescription
rhsRationaldivisor

Return

TypeDescription
BigIntquotient

IsWhole #

Whether this is a whole number, i.e. the reduced denominator is one.

method : public : IsWhole() ~ Bool

Return

TypeDescription
Booltrue if whole

IsZero #

Whether the value is zero.

method : public : IsZero() ~ Bool

Return

TypeDescription
Booltrue if zero

LessEqual #

Whether this value is less than or equal to the argument.

method : public : LessEqual(rhs:Rational) ~ Bool

Parameters

NameTypeDescription
rhsRationalvalue to compare against

Return

TypeDescription
Booltrue if less or equal

LessThan #

Whether this value is less than the argument.

method : public : LessThan(rhs:Rational) ~ Bool

Parameters

NameTypeDescription
rhsRationalvalue to compare against

Return

TypeDescription
Booltrue if less

Modulo #

Modulo, satisfying a = IntegerDivide(a,b) * b + Modulo(a,b). The result takes the divisor's sign, which flooring division implies.

method : public : Modulo(rhs:Rational) ~ Rational

Parameters

NameTypeDescription
rhsRationaldivisor

Return

TypeDescription
Rationalremainder

Multiply #

The product, reduced to lowest terms.

method : public : Multiply(rhs:Rational) ~ Rational

Parameters

NameTypeDescription
rhsRationalvalue to multiply by

Return

TypeDescription
Rationalthe product, or Nil if rhs is Nil

Multiply #

The product with a whole number.

method : public : Multiply(whole:Int) ~ Rational

Parameters

NameTypeDescription
wholeIntvalue to multiply by

Return

TypeDescription
Rationalthe product

Negate #

The value with its sign reversed. Zero negates to itself.

method : public : Negate() ~ Rational

Return

TypeDescription
Rationalthe negation

Of # function

Builds num/den in lowest terms. A zero denominator yields Nil, because there is no value to represent -- unlike an `Int`-backed fraction there is no overflow case to signal, so Nil is the only failure.

function : Of(num:BigInt, den:BigInt) ~ Rational

Parameters

NameTypeDescription
numBigIntnumerator
denBigIntdenominator

Of # function

Builds num/den in lowest terms from two Int values.

function : Of(num:Int, den:Int) ~ Rational

Parameters

NameTypeDescription
numIntnumerator
denIntdenominator

Return

TypeDescription
Rationalthe fraction, or Nil if den is zero

Of # function

Coerces a whole number.

function : Of(whole:Int) ~ Rational

Parameters

NameTypeDescription
wholeIntvalue

Of # function

Coerces a BigInt to a whole fraction.

function : Of(whole:BigInt) ~ Rational

Parameters

NameTypeDescription
wholeBigIntvalue

Return

TypeDescription
Rationalthe fraction, or Nil if whole is Nil

One # function

One.

function : One() ~ Rational

Return

TypeDescription
Rationalone

Parse # function

Parses `n`, `n/d` or a signed form of either.

function : Parse(text:String) ~ Rational

Parameters

NameTypeDescription
textStringtext

Return

TypeDescription
Rationalthe value, or Nil if the text is not a fraction

Pow #

This value raised to an integer power. A negative exponent inverts, so zero raised to one yields Nil.

method : public : Pow(e:Int) ~ Rational

Parameters

NameTypeDescription
eIntexponent

Return

TypeDescription
Rationalthe power

Reciprocal #

The multiplicative inverse. Zero has none, so this returns Nil for it.

method : public : Reciprocal() ~ Rational

Return

TypeDescription
Rationalreciprocal

Sign #

-1, 0 or 1 as the value is negative, zero or positive.

method : public : Sign() ~ Int

Return

TypeDescription
Intsign

Subtract #

The difference, reduced to lowest terms.

method : public : Subtract(rhs:Rational) ~ Rational

Parameters

NameTypeDescription
rhsRationalvalue to subtract

Return

TypeDescription
Rationalthe difference, or Nil if rhs is Nil

Subtract #

The difference with a whole number.

method : public : Subtract(whole:Int) ~ Rational

Parameters

NameTypeDescription
wholeIntvalue to subtract

Return

TypeDescription
Rationalthe difference

ToFloat #

The value as a `Float`. Lossy by nature, and Nil-free: a fraction too large for a `Float` yields an infinity rather than an error.

method : public : ToFloat() ~ Float

Return

TypeDescription
Floatvalue

ToString #

`n/d`, or just `n` when the denominator is one. Negative values carry the sign on the numerator.

method : public : ToString() ~ String

Return

TypeDescription
Stringtext

Zero # function

Zero.

function : Zero() ~ Rational

Return

TypeDescription
Rationalzero