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.
Example
f := BigInt->New(1);
each(i : 25) { f := f->Multiply(BigInt->New(i + 1)); };
f->ToString()->PrintLine(); # 25! = 15511210043330985984000000Operations
- New
- Abs
- Add
- Compare
- DigitCount
- DivMod
- Divide
- Equals
- FitsInt
- Gcd
- GreaterThan
- HashID
- IsZero
- LessThan
- Modulo
- Multiply
- Negate
- One
- Parse
- Pow
- Sign
- Subtract
- ToInt
- ToString
- Zero
Abs #
The absolute value.
method : public : Abs() ~ BigIntReturn
| Type | Description |
|---|---|
| BigInt | the magnitude, as a non-negative value |
Add #
The sum. Exact at any size.
method : public : Add(rhs:BigInt) ~ BigIntParameters
| Name | Type | Description |
|---|---|---|
| rhs | BigInt | value to add |
Return
| Type | Description |
|---|---|
| BigInt | the 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) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| rhs | Compare | other value |
Return
| Type | Description |
|---|---|
| Int | ordering |
DigitCount #
The number of decimal digits, not counting a sign.
method : public : DigitCount() ~ IntReturn
| Type | Description |
|---|---|
| Int | digit 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() ~ BigIntParameters
| Name | Type | Description |
|---|---|---|
Divide #
Truncating division: the quotient rounds toward zero, matching `Int`'s `/`. Returns Nil for a zero divisor.
method : public : Divide(rhs:BigInt) ~ BigIntParameters
| Name | Type | Description |
|---|---|---|
| rhs | BigInt | divisor |
Return
| Type | Description |
|---|---|
| BigInt | quotient |
Equals #
Whether two values are equal.
method : public : Equals(rhs:BigInt) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| rhs | BigInt | value to compare against |
Return
| Type | Description |
|---|---|
| Bool | true if equal |
FitsInt #
Whether the value fits in an `Int`.
method : public : FitsInt() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true 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) ~ BigIntParameters
| Name | Type | Description |
|---|---|---|
| rhs | BigInt | other value |
Return
| Type | Description |
|---|---|
| BigInt | greatest common divisor |
GreaterThan #
Whether this value is greater than the argument.
method : public : GreaterThan(rhs:BigInt) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| rhs | BigInt | value to compare against |
Return
| Type | Description |
|---|---|
| Bool | true 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() ~ IntReturn
| Type | Description |
|---|---|
| Int | hash |
IsZero #
Whether the value is zero.
method : public : IsZero() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if zero |
LessThan #
Whether this value is less than the argument.
method : public : LessThan(rhs:BigInt) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| rhs | BigInt | value to compare against |
Return
| Type | Description |
|---|---|
| Bool | true 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) ~ BigIntParameters
| Name | Type | Description |
|---|---|---|
| rhs | BigInt | divisor |
Return
| Type | Description |
|---|---|
| BigInt | remainder |
Multiply #
The product. Schoolbook multiplication, so the cost is the product of the operands' lengths.
method : public : Multiply(rhs:BigInt) ~ BigIntParameters
| Name | Type | Description |
|---|---|---|
| rhs | BigInt | value to multiply by |
Return
| Type | Description |
|---|---|
| BigInt | the product, or Nil if rhs is Nil |
Negate #
The value with its sign reversed. Zero negates to itself.
method : public : Negate() ~ BigIntReturn
| Type | Description |
|---|---|
| BigInt | the negation |
New # constructor
Builds a value from an `Int`.
New(value:Int)Parameters
| Name | Type | Description |
|---|---|---|
| value | Int | value |
Parse # function
Parses a decimal string, with an optional leading `-` or `+`.
function : Parse(text:String) ~ BigIntParameters
| Name | Type | Description |
|---|---|---|
| text | String | text |
Return
| Type | Description |
|---|---|
| BigInt | the 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) ~ BigIntParameters
| Name | Type | Description |
|---|---|---|
| e | Int | exponent |
Return
| Type | Description |
|---|---|
| BigInt | the power, or Nil if the exponent is negative |
Sign #
-1, 0 or 1 as this value is negative, zero or positive.
method : public : Sign() ~ IntReturn
| Type | Description |
|---|---|
| Int | sign |
Subtract #
The difference.
method : public : Subtract(rhs:BigInt) ~ BigIntParameters
| Name | Type | Description |
|---|---|---|
| rhs | BigInt | value to subtract |
Return
| Type | Description |
|---|---|
| BigInt | the 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() ~ IntReturn
| Type | Description |
|---|---|
| Int | value |