Complex and Rational Numbers

Julia ships with predefined types representing both complex and rational numbers, and supports all standard mathematical operations on them. Conversion and Promotion are defined so that operations on any combination of predefined numeric types, whether primitive or composite, behave as expected.

Complex Numbers

The global constant im is bound to the complex number i, representing the principal square root of -1. It was deemed harmful to co-opt the name i for a global constant, since it is such a popular index variable name. Since Julia allows numeric literals to be juxtaposed with identifiers as coefficients, this binding suffices to provide convenient syntax for complex numbers, similar to the traditional mathematical notation:


You can perform all the standard arithmetic operations with complex numbers:


The promotion mechanism ensures that combinations of operands of different types just work:


Note that 3/4im==3/(4im)==-(3/4)im, since a literal coefficient binds more tightly than division.

Standard functions to manipulate complex values are provided:


As usual, the absolute value (abs()) of a complex number is its distance from zero. abs2() gives the square of the absolute value, and is of particular use for complex numbers where it avoids taking a square root. angle() returns the phase angle in radians (also known as the argument or arg function). The full gamut of other Elementary Functions is also defined for complex numbers:


Note that mathematical functions typically return real values when applied to real numbers and complex values when applied to complex numbers. For example, sqrt() behaves differently when applied to -1 versus -1+0im even though -1==-1+0im:


The literal numeric coefficient notation does not work when constructing complex number from variables. Instead, the multiplication must be explicitly written out:


However, this is not recommended; Use the complex() function instead to construct a complex value directly from its real and imaginary parts.:


This construction avoids the multiplication and addition operations.

Inf and NaN propagate through complex numbers in the real and imaginary parts of a complex number as described in the Special floating-point values section:


Rational Numbers

Julia has a rational number type to represent exact ratios of integers. Rationals are constructed using the // operator:


If the numerator and denominator of a rational have common factors, they are reduced to lowest terms such that the denominator is non-negative:


This normalized form for a ratio of integers is unique, so equality of rational values can be tested by checking for equality of the numerator and denominator. The standardized numerator and denominator of a rational value can be extracted using the num() and den() functions:


Direct comparison of the numerator and denominator is generally not necessary, since the standard arithmetic and comparison operations are defined for rational values:


Rationals can be easily converted to floating-point numbers:


Conversion from rational to floating-point respects the following identity for any integral values of a and b, with the exception of the case a==0 and b==0:


Constructing infinite rational values is acceptable:


Trying to construct a NaN rational value, however, is not:


As usual, the promotion system makes interactions with other numeric types effortless: