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`.

BigInt

An integer of arbitrary size, exact at every step.

Immutable: every operation returns a new value and none modifies its receiver, so a `BigInt` can be shared without copying.

Implements: Compare, Stringify

Example

f := BigInt->New(1);
each(i : 25) { f := f->Multiply(BigInt->New(i + 1)); };
f->ToString()->PrintLine();     # 25! = 15511210043330985984000000

Operations

Abs #

The absolute value.

method : public : Abs() ~ BigInt

Return

TypeDescription
BigIntthe magnitude, as a non-negative value

Add #

The sum. Exact at any size.

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

Parameters

NameTypeDescription
rhsBigIntvalue to add

Return

TypeDescription
BigIntthe sum, or Nil if rhs is Nil

Compare #

-1, 0 or 1 as this value is less than, equal to or greater than the argument.

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

Parameters

NameTypeDescription
rhsCompareother value

Return

TypeDescription
Intordering

DigitCount #

The number of decimal digits, not counting a sign.

method : public : DigitCount() ~ Int

Return

TypeDescription
Intdigit count

DivMod #

Truncating division, returning either the quotient or the remainder.

Schoolbook long division, one quotient limb at a time from the most significant end. The divisor is first scaled so its top limb is at least half the base: with that done, estimating a quotient limb from the top two limbs of the running remainder is wrong by at most two, so the correction loop below is bounded. Without the scaling the estimate can be off by a factor, and the loop degrades into a search.

method : private : DivMod() ~ BigInt

Parameters

NameTypeDescription

Divide #

Truncating division: the quotient rounds toward zero, matching `Int`'s `/`. Returns Nil for a zero divisor.

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

Parameters

NameTypeDescription
rhsBigIntdivisor

Return

TypeDescription
BigIntquotient

Equals #

Whether two values are equal.

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

Parameters

NameTypeDescription
rhsBigIntvalue to compare against

Return

TypeDescription
Booltrue if equal

FitsInt #

Whether the value fits in an `Int`.

method : public : FitsInt() ~ Bool

Return

TypeDescription
Booltrue if it fits

Gcd #

The greatest common divisor of the two magnitudes, by Euclid. Always non-negative, and zero only when both values are zero.

method : public : Gcd(rhs:BigInt) ~ BigInt

Parameters

NameTypeDescription
rhsBigIntother value

Return

TypeDescription
BigIntgreatest common divisor

GreaterThan #

Whether this value is greater than the argument.

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

Parameters

NameTypeDescription
rhsBigIntvalue to compare against

Return

TypeDescription
Booltrue if greater

HashID #

A hash consistent with Compare: the representation is canonical -- trimmed, with a zero never carrying a sign -- so two equal values always have identical limbs and therefore the same hash.

method : public : HashID() ~ Int

Return

TypeDescription
Inthash

IsZero #

Whether the value is zero.

method : public : IsZero() ~ Bool

Return

TypeDescription
Booltrue if zero

LessThan #

Whether this value is less than the argument.

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

Parameters

NameTypeDescription
rhsBigIntvalue to compare against

Return

TypeDescription
Booltrue if less

Modulo #

The remainder of truncating division, taking the dividend's sign -- matching `Int`'s `%`. Returns Nil for a zero divisor.

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

Parameters

NameTypeDescription
rhsBigIntdivisor

Return

TypeDescription
BigIntremainder

Multiply #

The product. Schoolbook multiplication, so the cost is the product of the operands' lengths.

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

Parameters

NameTypeDescription
rhsBigIntvalue to multiply by

Return

TypeDescription
BigIntthe product, or Nil if rhs is Nil

Negate #

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

method : public : Negate() ~ BigInt

Return

TypeDescription
BigIntthe negation

New # constructor

Builds a value from an `Int`.

New(value:Int)

Parameters

NameTypeDescription
valueIntvalue

One # function

One.

function : One() ~ BigInt

Return

TypeDescription
BigIntone

Parse # function

Parses a decimal string, with an optional leading `-` or `+`.

function : Parse(text:String) ~ BigInt

Parameters

NameTypeDescription
textStringtext

Return

TypeDescription
BigIntthe value, or Nil if the text is not a decimal integer

Pow #

This value raised to a non-negative power, by repeated squaring.

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

Parameters

NameTypeDescription
eIntexponent

Return

TypeDescription
BigIntthe power, or Nil if the exponent is negative

Sign #

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

method : public : Sign() ~ Int

Return

TypeDescription
Intsign

Subtract #

The difference.

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

Parameters

NameTypeDescription
rhsBigIntvalue to subtract

Return

TypeDescription
BigIntthe difference, or Nil if rhs is Nil

ToInt #

The value as an `Int`, or 0 if it does not fit. Check with `FitsInt` first; 0 is also a legitimate value, so the result alone cannot say.

method : public : ToInt() ~ Int

Return

TypeDescription
Intvalue

ToString #

The value in decimal, with a leading `-` when negative.

method : public : ToString() ~ String

Return

TypeDescription
Stringtext

Zero # function

Zero.

function : Zero() ~ BigInt

Return

TypeDescription
BigIntzero