In this chapter, we shall discuss rational and complex numbers.
Rational Numbers
Julia represents exact ratios of integers with the help of rational number type. Let us understand about rational numbers in Julia in further sections −
Constructing rational numbers
In Julia REPL, the rational numbers are constructed by using the operator //. Below given is the example for the same −
julia> 4//54//5
You can also extract the standardized numerator and denominator as follows −
julia> numerator(8//9)8julia> denominator(8//9)9
Converting to floating-point numbers
It is very easy to convert the rational numbers to floating-point numbers. Check out the following example −
julia> float(2//3)0.6666666666666666Converting rational to floating-point numbers does not loose the following identity for any integral values of A and B. For example:julia> A = 20; B = 30;julia> isequal(float(A//B), A/B)true
Complex Numbers
As we know that the global constant im, which represents the principal square root of -1, is bound to the complex number. This binding in Julia suffice to provide convenient syntax for complex numbers because Julia allows numeric literals to be contrasted with identifiers as coefficients.
julia> 2+3im2 + 3im
Performing Standard arithmetic operations
We can perform all the standard arithmetic operations on complex numbers. The example are given below −
julia> (2 + 3im)*(1 - 2im)8 - 1imjulia> (2 + 3im)/(1 - 2im)-0.8 + 1.4imjulia> (2 + 3im)+(1 - 2im)3 + 1imjulia> (2 + 3im)-(1 - 2im)1 + 5imjulia> (2 + 3im)^2-5 + 12imjulia> (2 + 3im)^2.6-23.375430842463754 + 15.527174176755075imjulia> 2(2 + 3im)4 + 6imjulia> 2(2 + 3im)^-2.0-0.059171597633136105 - 0.14201183431952663im
Combining different operands
The promotion mechanism in Julia ensures that combining different kind of operators works fine on complex numbers. Let us understand it with the help of the following example −
julia> 2(2 + 3im)4 + 6imjulia> (2 + 3im)-11 + 3imjulia> (2 + 3im)+0.72.7 + 3.0imjulia> (2 + 3im)-0.7im2.0 + 2.3imjulia> 0.89(2 + 3im)1.78 + 2.67imjulia> (2 + 3im)/21.0 + 1.5imjulia> (2 + 3im)/(1-3im)-0.7000000000000001 + 0.8999999999999999imjulia> 3im^30 - 3imjulia> 1+2/5im1.0 - 0.4im
Functions to manipulate complex values
In Julia, we can also manipulate the values of complex numbers with the help of standard functions. Below are given some example for the same −
julia> real(4+7im) #real part of complex number4julia> imag(4+7im) #imaginary part of complex number7julia> conj(4+7im) # conjugate of complex number4 - 7imjulia> abs(4+7im) # absolute value of complex number8.06225774829855julia> abs2(4+7im) #squared absolute value65julia> angle(4+7im) #phase angle in radians1.0516502125483738
Let us check out the use of Elementary Functions for complex numbers in the below example −
julia> sqrt(7im) #square root of imaginary part1.8708286933869707 + 1.8708286933869707imjulia> sqrt(4+7im) #square root of complex number2.455835677350843 + 1.4251767869809258imjulia> cos(4+7im) #cosine of complex number-358.40393224005317 + 414.96701031076253imjulia> exp(4+7im) #exponential of complex number41.16166839296141 + 35.87025288661357imjulia> sinh(4+7im) #Hyperbolic sine value of complex number20.573930095756726 + 17.941143007955223im