Complex and Rational Numbers

    The global constant is bound to the complex number i, representing the principal square root of -1. (Using mathematicians’ i or engineers’ j for this global constant were rejected since they are such popular index variable names.) 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:

    1. julia> (1 + 2im)*(2 - 3im)
    2. 8 + 1im
    3. julia> (1 + 2im)/(1 - 2im)
    4. -0.6 + 0.8im
    5. julia> (1 + 2im) + (1 - 2im)
    6. 2 + 0im
    7. julia> (-3 + 2im) - (5 - 1im)
    8. -8 + 3im
    9. julia> (-1 + 2im)^2
    10. -3 - 4im
    11. julia> (-1 + 2im)^2.5
    12. 2.729624464784009 - 6.9606644595719im
    13. julia> (-1 + 2im)^(1 + 1im)
    14. -0.27910381075826657 + 0.08708053414102428im
    15. julia> 3(2 - 5im)
    16. 6 - 15im
    17. julia> 3(2 - 5im)^2
    18. -63 - 60im
    19. julia> 3(2 - 5im)^-1.0
    20. 0.20689655172413796 + 0.5172413793103449im

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

    1. julia> 2(1 - 1im)
    2. 2 - 2im
    3. julia> (2 + 3im) - 1
    4. 1 + 3im
    5. julia> (1 + 2im) + 0.5
    6. 1.5 + 2.0im
    7. julia> (2 + 3im) - 0.5im
    8. 2.0 + 2.5im
    9. julia> 0.75(1 + 2im)
    10. 0.75 + 1.5im
    11. julia> (2 + 3im) / 2
    12. 1.0 + 1.5im
    13. julia> (1 - 3im) / (2 + 2im)
    14. -0.5 - 1.0im
    15. julia> 2im^2
    16. -2 + 0im
    17. julia> 1 + 3/4im
    18. 1.0 - 0.75im

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

    Standard functions to manipulate complex values are provided:

    1. 1 + 2im
    2. julia> real(1 + 2im) # real part of z
    3. 1
    4. julia> imag(1 + 2im) # imaginary part of z
    5. 2
    6. julia> conj(1 + 2im) # complex conjugate of z
    7. 1 - 2im
    8. julia> abs(1 + 2im) # absolute value of z
    9. 2.23606797749979
    10. julia> abs2(1 + 2im) # squared absolute value
    11. 5
    12. julia> angle(1 + 2im) # phase angle in radians
    13. 1.1071487177940904

    As usual, the absolute value () 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. 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:

    1. julia> sqrt(1im)
    2. 0.7071067811865476 + 0.7071067811865475im
    3. julia> sqrt(1 + 2im)
    4. 1.272019649514069 + 0.7861513777574233im
    5. julia> cos(1 + 2im)
    6. 2.0327230070196656 - 3.0518977991518im
    7. julia> exp(1 + 2im)
    8. -1.1312043837568135 + 2.4717266720048188im
    9. julia> sinh(1 + 2im)
    10. -0.4890562590412937 + 1.4031192506220405im
    1. julia> sqrt(-1)
    2. ERROR: DomainError with -1.0:
    3. sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
    4. Stacktrace:
    5. [...]
    6. julia> sqrt(-1 + 0im)
    7. 0.0 + 1.0im

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

    However, this is not recommended. Instead, use the more efficient complex function to construct a complex value directly from its real and imaginary parts:

    1. julia> a = 1; b = 2; complex(a, b)
    2. 1 + 2im

    This construction avoids the multiplication and addition operations.

    and NaN propagate through complex numbers in the real and imaginary parts of a complex number as described in the section:

    1. julia> 1 + Inf*im
    2. 1.0 + Inf*im
    3. julia> 1 + NaN*im
    4. 1.0 + NaN*im

    Rational Numbers

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

    1. julia> 2//3
    2. 2//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:

    1. julia> 6//9
    2. 2//3
    3. julia> -4//8
    4. -1//2
    5. julia> 5//-15
    6. julia> -4//-12
    7. 1//3
    1. julia> numerator(2//3)
    2. 2
    3. julia> denominator(2//3)
    4. 3

    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 easily be converted to floating-point numbers:

    1. julia> float(3//4)
    2. 0.75

    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:

    1. julia> a = 1; b = 2;
    2. julia> isequal(float(a//b), a/b)
    3. true

    Constructing infinite rational values is acceptable:

    1. julia> 5//0
    2. 1//0
    3. julia> -3//0
    4. -1//0
    5. julia> typeof(ans)
    6. Rational{Int64}

    Trying to construct a rational value, however, is invalid:

    1. julia> 0//0
    2. ERROR: ArgumentError: invalid rational: zero(Int64)//zero(Int64)
    3. Stacktrace:
    4. [...]

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

    1. julia> 3//5 + 1
    2. 8//5
    3. julia> 3//5 - 0.5
    4. 0.09999999999999998
    5. julia> 2//7 * (1 + 2im)
    6. 2//7 + 4//7*im
    7. julia> 2//7 * (1.5 + 2im)
    8. 0.42857142857142855 + 0.5714285714285714im
    9. julia> 3//2 / (1 + 2im)
    10. 3//10 - 3//5*im
    11. julia> 1//2 + 2im
    12. 1//2 + 2//1*im
    13. julia> 1 + 2//3im
    14. 1//1 - 2//3*im
    15. julia> 0.5 == 1//2
    16. true
    17. julia> 0.33 == 1//3
    18. false
    19. julia> 0.33 < 1//3
    20. true