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

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.

Implements: Stringify

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.000000i

Operations

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() ~ Float

Return

TypeDescription
Floatmagnitude

Add #

The sum.

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

Parameters

NameTypeDescription
rhsComplexvalue to add

Return

TypeDescription
Complexthe sum, or Nil if rhs is Nil

Arg #

The argument (phase angle) in radians, in the range -pi to pi.

method : public : Arg() ~ Float

Return

TypeDescription
Floatangle

Conjugate #

The complex conjugate, re - im*i.

method : public : Conjugate() ~ Complex

Return

TypeDescription
Complexthe 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) ~ Complex

Parameters

NameTypeDescription
rhsComplexdivisor

Return

TypeDescription
Complexthe 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) ~ Bool

Parameters

NameTypeDescription
rhsComplexvalue to compare against

Return

TypeDescription
Booltrue if both parts are equal

GetImaginary #

The imaginary part.

method : public : GetImaginary() ~ Float

Return

TypeDescription
Floatimaginary part

GetReal #

The real part.

method : public : GetReal() ~ Float

Return

TypeDescription
Floatreal part

I # function

The imaginary unit i.

function : I() ~ Complex

Return

TypeDescription
Complexi

IsZero #

Whether both parts are zero.

method : public : IsZero() ~ Bool

Return

TypeDescription
Booltrue if zero

Multiply #

The product.

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

Parameters

NameTypeDescription
rhsComplexvalue to multiply by

Return

TypeDescription
Complexthe product, or Nil if rhs is Nil

Multiply #

The product with a real scalar.

method : public : Multiply(k:Float) ~ Complex

Parameters

NameTypeDescription
kFloatscalar

Return

TypeDescription
Complexthe scaled value

Negate #

The value with both parts' signs reversed.

method : public : Negate() ~ Complex

Return

TypeDescription
Complexthe negation

New # constructor

Builds re + im*i.

New(re:Float, im:Float)

Parameters

NameTypeDescription
reFloatreal part
imFloatimaginary part

New # constructor

Builds a real number, with a zero imaginary part.

New(re:Float)

Parameters

NameTypeDescription
reFloatreal part

One # function

One.

function : One() ~ Complex

Return

TypeDescription
Complexone

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) ~ Complex

Parameters

NameTypeDescription
rFloatmagnitude
thetaFloatangle in radians

Return

TypeDescription
Complexthe value

Reciprocal #

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

method : public : Reciprocal() ~ Complex

Return

TypeDescription
Complexreciprocal, or Nil if this value is zero

Subtract #

The difference.

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

Parameters

NameTypeDescription
rhsComplexvalue to subtract

Return

TypeDescription
Complexthe difference, or Nil if rhs is Nil

ToString #

`a + bi` or `a - bi`, each part formatted as `Float->ToString` formats it.

method : public : ToString() ~ String

Return

TypeDescription
Stringtext

Zero # function

Zero.

function : Zero() ~ Complex

Return

TypeDescription
Complexzero