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`.
Complex
A complex number with `Float` real and imaginary parts.
Immutable: every operation returns a new value and none modifies its receiver. Division and the reciprocal return Nil for a zero divisor rather than raising, because a `Float` division by zero aborts the program.
Example
# (1 + 2i)(3 - 4i) = 11 + 2i
z := Complex->New(1.0, 2.0)->Multiply(Complex->New(3.0, -4.0));
z->ToString()->PrintLine(); # 11.000000 + 2.000000iOperations
- New
- Abs
- Add
- Arg
- Conjugate
- Divide
- Equals
- GetImaginary
- GetReal
- I
- IsZero
- Multiply
- Negate
- One
- Polar
- Reciprocal
- Subtract
- ToString
- Zero
Abs #
The magnitude sqrt(re^2 + im^2), scaled so that it neither overflows nor underflows when the parts are large or tiny but the result is not.
method : public : Abs() ~ FloatReturn
| Type | Description |
|---|---|
| Float | magnitude |
Add #
The sum.
method : public : Add(rhs:Complex) ~ ComplexParameters
| Name | Type | Description |
|---|---|---|
| rhs | Complex | value to add |
Return
| Type | Description |
|---|---|
| Complex | the sum, or Nil if rhs is Nil |
Arg #
The argument (phase angle) in radians, in the range -pi to pi.
method : public : Arg() ~ FloatReturn
| Type | Description |
|---|---|
| Float | angle |
Conjugate #
The complex conjugate, re - im*i.
method : public : Conjugate() ~ ComplexReturn
| Type | Description |
|---|---|
| Complex | the conjugate |
Divide #
The quotient. Uses Smith's method, which scales by the larger part of the divisor so the intermediate does not overflow or underflow the way (c^2 + d^2) can.
method : public : Divide(rhs:Complex) ~ ComplexParameters
| Name | Type | Description |
|---|---|---|
| rhs | Complex | divisor |
Return
| Type | Description |
|---|---|
| Complex | the quotient, or Nil if rhs is Nil or zero |
Equals #
Whether two values are exactly equal, part by part. Floating-point results rarely compare exactly; compare `Subtract(...)->Abs()` against a tolerance when the values were computed.
method : public : Equals(rhs:Complex) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| rhs | Complex | value to compare against |
Return
| Type | Description |
|---|---|
| Bool | true if both parts are equal |
GetImaginary #
The imaginary part.
method : public : GetImaginary() ~ FloatReturn
| Type | Description |
|---|---|
| Float | imaginary part |
IsZero #
Whether both parts are zero.
method : public : IsZero() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if zero |
Multiply #
The product.
method : public : Multiply(rhs:Complex) ~ ComplexParameters
| Name | Type | Description |
|---|---|---|
| rhs | Complex | value to multiply by |
Return
| Type | Description |
|---|---|
| Complex | the product, or Nil if rhs is Nil |
Multiply #
The product with a real scalar.
method : public : Multiply(k:Float) ~ ComplexParameters
| Name | Type | Description |
|---|---|---|
| k | Float | scalar |
Return
| Type | Description |
|---|---|
| Complex | the scaled value |
Negate #
The value with both parts' signs reversed.
method : public : Negate() ~ ComplexReturn
| Type | Description |
|---|---|
| Complex | the negation |
New # constructor
Builds re + im*i.
New(re:Float, im:Float)Parameters
| Name | Type | Description |
|---|---|---|
| re | Float | real part |
| im | Float | imaginary part |
New # constructor
Builds a real number, with a zero imaginary part.
New(re:Float)Parameters
| Name | Type | Description |
|---|---|---|
| re | Float | real part |
Polar # function
Builds a value from polar form: magnitude r at angle theta, i.e. r(cos theta + i sin theta). `Polar(1.0, theta)` is e^(i theta).
function : Polar(r:Float, theta:Float) ~ ComplexParameters
| Name | Type | Description |
|---|---|---|
| r | Float | magnitude |
| theta | Float | angle in radians |
Return
| Type | Description |
|---|---|
| Complex | the value |
Reciprocal #
The multiplicative inverse, 1/z. Zero has none, so this returns Nil for it.
method : public : Reciprocal() ~ ComplexReturn
| Type | Description |
|---|---|
| Complex | reciprocal, or Nil if this value is zero |
Subtract #
The difference.
method : public : Subtract(rhs:Complex) ~ ComplexParameters
| Name | Type | Description |
|---|---|---|
| rhs | Complex | value to subtract |
Return
| Type | Description |
|---|---|
| Complex | the difference, or Nil if rhs is Nil |