Arithmetic/Complex: Difference between revisions

add Common Lisp example
(add Common Lisp example)
Line 162:
}
</cpp>
 
=={{header|Common Lisp}}==
 
Complex numbers are a built-in numeric type in Common Lisp. The literal syntax for a complex number is <code>#C(<var>real</var> <var>imaginary</var>)</code>. The components of a complex number may be integers, ratios, or floating-point. Arithmetic operations automatically return complex (or real) numbers when appropriate:
 
> (sqrt -1)
#C(0.0 1.0)
 
> (expt #c(0 1) 2)
-1
 
Here are some arithmetic operations on complex numbers:
 
> (+ #c(0 1) #c(1 0))
#C(1 1)
> (* #c(1 1) 2)
#C(2 2)
> (* #c(1 1) #c(0 2))
#C(-2 2)
> (- #c(1 1))
#C(-1 -1)
> (/ #c(0 2))
#C(0 -1/2)
 
Complex numbers can be constructed from real and imaginary parts using the <code>complex</code> function, and taken apart using the <code>realpart</code> and <code>imagpart</code> functions.
 
> (complex 64 (/ 3 4))
#C(64 3/4)
 
> (realpart #c(5 5))
5
 
> (imagpart (complex 0 pi))
3.141592653589793d0
 
=={{header|D}}==