complex
complex Function
Syntax:
complex realpart &optional imagpart → complex
Arguments and Values:
realpart—a real.
imagpart—a real.
complex—a rational or a complex .
Description:
complex returns a number whose real part is realpart and whose imaginary part is imagpart.
If realpart is a rational and imagpart is the rational number zero, the result of complex is realpart, a rational. Otherwise, the result is a complex .
If either realpart or imagpart is a float, the non-float is converted to a float before the complex is created. If imagpart is not supplied, the imaginary part is a zero of the same type as realpart; i.e., (coerce 0 (type-of realpart)) is effectively used.
Type upgrading implies a movement upwards in the type hierarchy lattice. In the case of complexes, the type-specifier must be a subtype of (upgraded-complex-part-type type-specifier). If type-specifier1 is a subtype of type-specifier2, then (upgraded-complex-element-type ’type specifier1) must also be a subtype of (upgraded-complex-element-type ’type-specifier2). Two disjoint types can be upgraded into the same thing.
Examples:
(complex 0) → 0
(complex 0.0) → #C(0.0 0.0)
(complex 1 1/2) → #C(1 1/2)
(complex 1 .99) → #C(1.0 0.99)
(complex 3/2 0.0) → #C(1.5 0.0)
See Also:
realpart, imagpart, Section 2.4.8.11 (Sharpsign C)
Expanded Reference: complex
Creating complex numbers
complex creates a number from real and imaginary parts. If the imaginary part is a rational zero, the result is simply the real part as a rational.
(complex 3 4)
=> #C(3 4)
(complex 1 0)
=> 1
(complex 1 1/2)
=> #C(1 1/2)
Behavior with floating-point parts
If either part is a float, the non-float part is converted to a float. Even with a zero imaginary part, a float input yields a complex.
(complex 0.0)
=> #C(0.0 0.0)
(complex 1.0 0.0)
=> #C(1.0 0.0)
(complex 1 .99)
=> #C(1.0 0.99)
(complex 3/2 0.0)
=> #C(1.5 0.0)
Omitting the imaginary part
When imagpart is not supplied, it defaults to zero of the same type as the real part.
(complex 5)
=> 5
(complex 5.0)
=> #C(5.0 0.0)
(complex 1/3)
=> 1/3
Round-tripping with realpart and imagpart
You can decompose and reconstruct complex numbers.
(let ((z #c(3 4)))
(complex (realpart z) (imagpart z)))
=> #C(3 4)
(let ((z #c(1.5 -2.5)))
(complex (realpart z) (imagpart z)))
=> #C(1.5 -2.5)
Double-float complex numbers
Using double-float components produces double-float complex numbers.
(complex 1.0d0 2.0d0)
=> #C(1.0d0 2.0d0)
(complex 1 2.0d0)
=> #C(1.0d0 2.0d0)