Skip to content

TM021 Complex Numbers

Dorai Sitaram edited this page Dec 27, 2015 · 4 revisions

Proposed complex-number literal syntax for Pyret

Pyret numbers currently are rationals (including integers) and roughnums. Rationals are arbitrary-precision. Roughnums correspond to JavaScript doubles and are implicitly considered to be approximations. Rational literals are specified as:

<optional-sign> <unsigned-integer>
<optional-sign> <unsigned-integer> <exponent>
<optional-sign> <unsigned-decimal>
<optional-sign> <unsigned-decimal> <exponent>
<optional-sign> <unsigned-integer> <solidus> <unsigned-integer>

Roughum literals:

<tilde> <optional-sign> <unsigned-rational>

except that the solidus notation is disallowed. Note that roughnums are limited to JS doubles, so the <unsigned-rational> in such a literal is constrained to what is possible.

Into this mix, we can add complex rationals (aka precise complex numbers or gaussian rationals). Complex rational literals can be rectangular:

<optional-sign> <unsigned-rational> <sign> <unsigned-rational> <i>

Or polar:

<optional-sign> <unsigned-rational> <at-sign> <optional-sign> <unsigned-rational>

In both cases, the solidus notation is allowed.

Here are some example complex rationals:

12+34i
+12+34i
-12+34i
-12-34i
1/2+3/4i
-1/2-3/4i
12e34+56e78
12.34+56.78i
12.34e-56+78.90e-12i
1/2+34.56e-78i
12@2
-12@2
-12@-2.3
12e4@-2.3e2
1/2@4/5

Complex roughnums prefix a tilde:

<tilde> <optional-sign> <unsigned-rational> <sign> <unsigned-rational> <i>
<tilde> <optional-sign> <unsigned-rational> <at-sign> <unsigned-rational>

Here the solidus notation is not allowed. Again, the <unsigned-rational> is limited to what is possible with a JS double.

Example complex roughnums:

~12+34i
~+12+34i
~-12+34i
~-12-34i
~12e34+56e78
~12.34+56.78i
~12.34e-56+78.90e-12i
~12@2
~-12@2
~-12@-2.3
~12e4@-2.3e2
Note
In the exponent, the e can be E, and the imaginary-part marker i can also be I, j, or J.
Note
Internally, Pyret stores complex numbers (of both varieties) in rectangular format, even if the literal is in polar notation. Furthermore, a polar complex-rational literal is converted to a rectangular complex-rational (rather than a complex-roughnum), even though the translation from polar to rectangular doesn’t really preserve rationality. This is in keeping with Scheme (tradition?).

New complex-specific functions

In addition to expanding the behavior of existing functions to tackle complex numbers as both domain and range, the following new functions are introduced:

Complex-destructuring operations

num-realpart

returns the real part of a complex number.

num-imagpart

returns the imaginary part of a complex number.

num-conjugate

returns the conjugate of a complex number.

num-magnitude

returns the magnitude of a complex number.

num-angle

returns the angle of a complex number.

Type Predicates

num-is-complexrational

returns true for numbers that are exact (whether real or complex), i.e., true for integers (whether JSnative or bignum), nonintegral rationals, nonreal complex numbers with rational parts.

num-is-complexroughnum

returns true for numbers that are inexact (whether real or complex).

Casting operations

num-to-complexrational

removes inexactness.

num-to-complexroughnum

removes exactness.