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.
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/60Operations
- Abs
- Add
- Ceiling
- Compare
- Divide
- Equals
- Floor
- GetDenominator
- GetNumerator
- GreaterEqual
- GreaterThan
- HashID
- IntegerDivide
- IsWhole
- IsZero
- LessEqual
- LessThan
- Modulo
- Multiply
- Negate
- Of
- One
- Parse
- Pow
- Reciprocal
- Sign
- Subtract
- ToFloat
- ToString
- Zero
Abs #
The absolute value.
method : public : Abs() ~ RationalReturn
| Type | Description |
|---|---|
| Rational | the magnitude |
Add #
The sum, reduced to lowest terms.
method : public : Add(rhs:Rational) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | value to add |
Return
| Type | Description |
|---|---|
| Rational | the sum, or Nil if rhs is Nil |
Add #
The sum with a whole number.
method : public : Add(whole:Int) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| whole | Int | value to add |
Return
| Type | Description |
|---|---|
| Rational | the sum |
Ceiling #
The least integer not less than this value.
method : public : Ceiling() ~ BigIntReturn
| Type | Description |
|---|---|
| BigInt | ceiling |
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) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| rhs | Compare | other value |
Return
| Type | Description |
|---|---|
| Int | ordering |
Divide #
Division. Dividing by zero yields Nil.
method : public : Divide(rhs:Rational) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | divisor |
Return
| Type | Description |
|---|---|
| Rational | quotient |
Divide #
Division by a whole number.
method : public : Divide(whole:Int) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| whole | Int | divisor |
Return
| Type | Description |
|---|---|
| Rational | the 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) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | value to compare against |
Return
| Type | Description |
|---|---|
| Bool | true 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() ~ BigIntReturn
| Type | Description |
|---|---|
| BigInt | floor |
GetDenominator #
The denominator, in lowest terms and always positive.
method : public : GetDenominator() ~ BigIntReturn
| Type | Description |
|---|---|
| BigInt | denominator |
GetNumerator #
The numerator, in lowest terms, carrying the sign.
method : public : GetNumerator() ~ BigIntReturn
| Type | Description |
|---|---|
| BigInt | numerator |
GreaterEqual #
Whether this value is greater than or equal to the argument.
method : public : GreaterEqual(rhs:Rational) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | value to compare against |
Return
| Type | Description |
|---|---|
| Bool | true if greater or equal |
GreaterThan #
Whether this value is greater than the argument.
method : public : GreaterThan(rhs:Rational) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | value to compare against |
Return
| Type | Description |
|---|---|
| Bool | true if greater |
HashID #
A hash consistent with Compare: lowest terms are unique, so equal values have identical numerators and denominators.
method : public : HashID() ~ IntReturn
| Type | Description |
|---|---|
| Int | hash |
IntegerDivide #
Flooring integer division: the quotient rounded toward negative infinity.
method : public : IntegerDivide(rhs:Rational) ~ BigIntParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | divisor |
Return
| Type | Description |
|---|---|
| BigInt | quotient |
IsWhole #
Whether this is a whole number, i.e. the reduced denominator is one.
method : public : IsWhole() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if whole |
IsZero #
Whether the value is zero.
method : public : IsZero() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if zero |
LessEqual #
Whether this value is less than or equal to the argument.
method : public : LessEqual(rhs:Rational) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | value to compare against |
Return
| Type | Description |
|---|---|
| Bool | true if less or equal |
LessThan #
Whether this value is less than the argument.
method : public : LessThan(rhs:Rational) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | value to compare against |
Return
| Type | Description |
|---|---|
| Bool | true 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) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | divisor |
Return
| Type | Description |
|---|---|
| Rational | remainder |
Multiply #
The product, reduced to lowest terms.
method : public : Multiply(rhs:Rational) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | value to multiply by |
Return
| Type | Description |
|---|---|
| Rational | the product, or Nil if rhs is Nil |
Multiply #
The product with a whole number.
method : public : Multiply(whole:Int) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| whole | Int | value to multiply by |
Return
| Type | Description |
|---|---|
| Rational | the product |
Negate #
The value with its sign reversed. Zero negates to itself.
method : public : Negate() ~ RationalReturn
| Type | Description |
|---|---|
| Rational | the 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) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| num | BigInt | numerator |
| den | BigInt | denominator |
Of # function
Builds num/den in lowest terms from two Int values.
function : Of(num:Int, den:Int) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| num | Int | numerator |
| den | Int | denominator |
Return
| Type | Description |
|---|---|
| Rational | the fraction, or Nil if den is zero |
Of # function
Coerces a whole number.
function : Of(whole:Int) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| whole | Int | value |
Of # function
Coerces a BigInt to a whole fraction.
function : Of(whole:BigInt) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| whole | BigInt | value |
Return
| Type | Description |
|---|---|
| Rational | the fraction, or Nil if whole is Nil |
Parse # function
Parses `n`, `n/d` or a signed form of either.
function : Parse(text:String) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| text | String | text |
Return
| Type | Description |
|---|---|
| Rational | the 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) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| e | Int | exponent |
Return
| Type | Description |
|---|---|
| Rational | the power |
Reciprocal #
The multiplicative inverse. Zero has none, so this returns Nil for it.
method : public : Reciprocal() ~ RationalReturn
| Type | Description |
|---|---|
| Rational | reciprocal |
Sign #
-1, 0 or 1 as the value is negative, zero or positive.
method : public : Sign() ~ IntReturn
| Type | Description |
|---|---|
| Int | sign |
Subtract #
The difference, reduced to lowest terms.
method : public : Subtract(rhs:Rational) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| rhs | Rational | value to subtract |
Return
| Type | Description |
|---|---|
| Rational | the difference, or Nil if rhs is Nil |
Subtract #
The difference with a whole number.
method : public : Subtract(whole:Int) ~ RationalParameters
| Name | Type | Description |
|---|---|---|
| whole | Int | value to subtract |
Return
| Type | Description |
|---|---|
| Rational | the 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() ~ FloatReturn
| Type | Description |
|---|---|
| Float | value |