Julia includes predefined types for both complex and rational numbers, and supports all the standardMathematical Operations and Elementary Functions on them.Conversion and Promotion are defined so that operations on any combination of predefined numeric types, whether primitive or composite, behave as expected.
The global constantim
is bound to the complex numberi, representing the principal square root of -1. (Using mathematicians'i
or engineers'j
for this global constant was rejected since they are such popular index variable names.) Since Julia allows numeric literals to bejuxtaposed with identifiers as coefficients, this binding suffices to provide convenient syntax for complex numbers, similar to the traditional mathematical notation:
julia> 1+2im1 + 2im
You can perform all the standard arithmetic operations with complex numbers:
julia> (1 + 2im)*(2 - 3im)8 + 1imjulia> (1 + 2im)/(1 - 2im)-0.6 + 0.8imjulia> (1 + 2im) + (1 - 2im)2 + 0imjulia> (-3 + 2im) - (5 - 1im)-8 + 3imjulia> (-1 + 2im)^2-3 - 4imjulia> (-1 + 2im)^2.52.729624464784009 - 6.9606644595719imjulia> (-1 + 2im)^(1 + 1im)-0.27910381075826657 + 0.08708053414102428imjulia> 3(2 - 5im)6 - 15imjulia> 3(2 - 5im)^2-63 - 60imjulia> 3(2 - 5im)^-1.00.20689655172413793 + 0.5172413793103449im
The promotion mechanism ensures that combinations of operands of different types just work:
julia> 2(1 - 1im)2 - 2imjulia> (2 + 3im) - 11 + 3imjulia> (1 + 2im) + 0.51.5 + 2.0imjulia> (2 + 3im) - 0.5im2.0 + 2.5imjulia> 0.75(1 + 2im)0.75 + 1.5imjulia> (2 + 3im) / 21.0 + 1.5imjulia> (1 - 3im) / (2 + 2im)-0.5 - 1.0imjulia> 2im^2-2 + 0imjulia> 1 + 3/4im1.0 - 0.75im
Note that3/4im == 3/(4*im) == -(3/4*im)
, since a literal coefficient binds more tightly than division.
Standard functions to manipulate complex values are provided:
julia> z = 1 + 2im1 + 2imjulia> real(1 + 2im) # real part of z1julia> imag(1 + 2im) # imaginary part of z2julia> conj(1 + 2im) # complex conjugate of z1 - 2imjulia> abs(1 + 2im) # absolute value of z2.23606797749979julia> abs2(1 + 2im) # squared absolute value5julia> angle(1 + 2im) # phase angle in radians1.1071487177940904
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 since it avoids taking a square root.angle
returns the phase angle in radians (also known as theargument orarg function). The full gamut of otherElementary Functions is also defined for complex numbers:
julia> sqrt(1im)0.7071067811865476 + 0.7071067811865475imjulia> sqrt(1 + 2im)1.272019649514069 + 0.7861513777574233imjulia> cos(1 + 2im)2.0327230070196656 - 3.0518977991517997imjulia> exp(1 + 2im)-1.1312043837568135 + 2.4717266720048188imjulia> sinh(1 + 2im)-0.4890562590412937 + 1.4031192506220405im
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
:
julia> sqrt(-1)ERROR: DomainError with -1.0:sqrt was called with a negative real argument but will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).Stacktrace:[...]julia> sqrt(-1 + 0im)0.0 + 1.0im
Theliteral numeric coefficient notation does not work when constructing a complex number from variables. Instead, the multiplication must be explicitly written out:
julia> a = 1; b = 2; a + b*im1 + 2im
However, this isnot recommended. Instead, use the more efficientcomplex
function to construct a complex value directly from its real and imaginary parts:
julia> a = 1; b = 2; complex(a, b)1 + 2im
This construction avoids the multiplication and addition operations.
Inf
andNaN
propagate through complex numbers in the real and imaginary parts of a complex number as described in theSpecial floating-point values section:
julia> 1 + Inf*im1.0 + Inf*imjulia> 1 + NaN*im1.0 + NaN*im
Julia has a rational number type to represent exact ratios of integers. Rationals are constructed using the//
operator:
julia> 2//32//3
If the numerator and denominator of a rational have common factors, they are reduced to lowest terms such that the denominator is non-negative:
julia> 6//92//3julia> -4//8-1//2julia> 5//-15-1//3julia> -4//-121//3
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 thenumerator
anddenominator
functions:
julia> numerator(2//3)2julia> denominator(2//3)3
Direct comparison of the numerator and denominator is generally not necessary, since the standard arithmetic and comparison operations are defined for rational values:
julia> 2//3 == 6//9truejulia> 2//3 == 9//27falsejulia> 3//7 < 1//2truejulia> 3//4 > 2//3truejulia> 2//4 + 1//62//3julia> 5//12 - 1//41//6julia> 5//8 * 3//125//32julia> 6//5 / 10//721//25
Rationals can easily be converted to floating-point numbers:
julia> float(3//4)0.75
Conversion from rational to floating-point respects the following identity for any integral values ofa
andb
, except whena==0 && b <= 0
:
julia> a = 1; b = 2;julia> isequal(float(a//b), a/b)truejulia> a, b = 0, 0(0, 0)julia> float(a//b)ERROR: ArgumentError: invalid rational: zero(Int64)//zero(Int64)Stacktrace:[...]julia> a/bNaNjulia> a, b = 0, -1(0, -1)julia> float(a//b), a/b(0.0, -0.0)
Constructing infinite rational values is acceptable:
julia> 5//01//0julia> x = -3//0-1//0julia> typeof(x)Rational{Int64}
Trying to construct aNaN
rational value, however, is invalid:
julia> 0//0ERROR: ArgumentError: invalid rational: zero(Int64)//zero(Int64)Stacktrace:[...]
As usual, the promotion system makes interactions with other numeric types effortless:
julia> 3//5 + 18//5julia> 3//5 - 0.50.09999999999999998julia> 2//7 * (1 + 2im)2//7 + 4//7*imjulia> 2//7 * (1.5 + 2im)0.42857142857142855 + 0.5714285714285714imjulia> 3//2 / (1 + 2im)3//10 - 3//5*imjulia> 1//2 + 2im1//2 + 2//1*imjulia> 1 + 2//3im1//1 - 2//3*imjulia> 0.5 == 1//2truejulia> 0.33 == 1//3falsejulia> 0.33 < 1//3truejulia> 1//3 - 0.330.0033333333333332993
Settings
This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.