x-lang

← Index

x/num/complex

Complex number arithmetic with rectangular and polar forms.

Literal syntax: a+bi, a-bi (e.g. 3+4i, 0+1i, 2-3i)

Extends arithmetic operators (+, -, *, /, =) with complex promotion.

make-complex

cx-imag-frac

cx-imag-dot

cx-imag-int

cx-sign

cx-real-frac

cx-real-dot

cx-neg

cx-real-int

Arithmetic

cx-add

cx-sub

cx-mul

cx-div

cx-eq

Constructors and Accessors

Operator Overrides

ensure-complex

complex-type

Predicates

number?

Test whether a value is any numeric type (integer, rational, float, or complex).

Parameters:

Returns: BOOL — True if x is a number

Examples:

(number? 3+4i) => #t

complex?

Test whether a value is any numeric type (alias for number?).

Parameters:

Returns: BOOL — True if x is a number

Examples:

(list (complex? 3+4i) (complex? 'a)) => (#t #f)

real?

Test whether a value is a real number (integer, rational, or float, but not complex).

Parameters:

Returns: BOOL — True if x is a real number

Examples:

(list (real? 1.5) (real? 3+4i)) => (#t #f)

Class Complex

(Complex type)

The complex type handle, for Convert and Type.

Returns: ATOM — The COMPLEX type handle

(Complex complex? x)

Test whether a value is any numeric type (alias for number?).

Parameters:

Returns: BOOL — True if x is a number

Examples:

(Complex complex? 2i) => #t

(Complex make re im)

Construct a complex number from rectangular coordinates.

Parameters:

Returns: COMPLEX|NUMBER — Complex number, or real if imaginary part is zero

Examples:

(Complex make 3 4) => 3+4i
(Complex make 3 0) => 3

(Complex from-polar mag ang)

Construct a complex number from polar coordinates (magnitude and angle).

Parameters:

Returns: COMPLEX|NUMBER — Complex number from polar coordinates

Examples:

(Complex from-polar 2 0) => 2.0+0.0i

(Complex real-part z)

Return the real part of a complex number, or the number itself for reals.

Parameters:

Returns: NUMBER — Real part

Examples:

(Complex real-part 3+4i) => 3

(Complex imag-part z)

Return the imaginary part of a complex number, or 0 for reals.

Parameters:

Returns: NUMBER — Imaginary part

Examples:

(Complex imag-part 3+4i) => 4
(Complex imag-part 7) => 0

(Complex magnitude z)

Return the magnitude (absolute value) of a complex or real number.

Parameters:

Returns: FLOAT — Absolute value (distance from origin)

Examples:

(Complex magnitude 3+4i) => 5.0

(Complex angle z)

Return the angle (argument) of a complex number in radians.

Parameters:

Returns: FLOAT — Angle in radians

Examples:

(Complex angle 0+1i) => 1.5707963267949

(Complex + a b)

Add two complex numbers (reals promote).

Parameters:

Returns: COMPLEX|NUMBER — Sum, collapsed to real if imaginary part is zero

Examples:

(Complex + 1+2i 3-1i) => 4+1i

(Complex - a b)

Subtract two complex numbers (reals promote).

Parameters:

Returns: COMPLEX|NUMBER — Difference, collapsed to real if imaginary part is zero

Examples:

(Complex - 3+4i 3) => 0+4i

(Complex * a b)

Multiply two complex numbers (reals promote).

Parameters:

Returns: COMPLEX|NUMBER — Product, collapsed to real if imaginary part is zero

Examples:

(Complex * 2i 2i) => -4

(Complex / a b)

Divide two complex numbers (reals promote).

Parameters:

Returns: COMPLEX|NUMBER — Quotient, collapsed to real if imaginary part is zero

Examples:

(Complex / 4+2i 2) => 2+1i

(Complex = a b)

Test whether two complex numbers are equal (reals promote).

Parameters:

Returns: BOOL — True if both real and imaginary parts are equal

Examples:

(Complex = 2 2+0i) => #t