Julia - Basic Operators

In this chapter, we shall discuss different types of operators in Julia.
In Julia, we get all the basic arithmetic operators across all the numeric primitive types. It also provides us bitwise operators as well as efficient implementation of comprehensive collection of standard mathematical functions.
Following table shows the basic arithmetic operators that are supported on Julia’s primitive numeric types −
The promotion system of Julia makes these arithmetic operations work naturally and automatically on the mixture of argument types.
Following example shows the use of arithmetic operators −
julia> 2+20-5
17

julia> 3-8
-5

julia> 50*2/10
10.0

julia> 23%2
1

julia> 2^4
16

Following table shows the bitwise operators that are supported on Julia’s primitive numeric types −
Following example shows the use of bitwise operators −
julia> ∼1009
-1010

julia> 12&23
4

julia> 12 & 23
4

julia> 12 | 23
31

julia> 12 ⊻ 23
27

julia> xor(12, 23)
27

julia> ∼UInt32(12)
0xfffffff3

julia> ∼UInt8(12)
0xf3

Each arithmetic as well as bitwise operator has an updating version which can be formed by placing an equal sign (=) immediately after the operator. This updating operator assigns the result of the operation back into its left operand. It means that a +=1 is equal to a = a+1.
Following is the list of the updating versions of all the binary arithmetic and bitwise operators −
+=
+=
-=
-=
*=
*=
/=
/=
\=
\=
÷=
÷=
%=
%=
^=
^=
&=
&=
|=
|=
⊻=
⊻=
>>>=
>>>=
>>=
>>=
<<=
<<=
Following example shows the use of updating operators −
julia> A = 100
100

julia> A +=100
200

julia> A
200

For each binary operation like ^, there is a corresponding “dot”(.) operation which is used on the entire array, one by one. For instance, if you would try to perform [1, 2, 3] ^ 2, then it is not defined and not possible to square an array. On the other hand, [1, 2, 3] .^ 2 is defined as computing the vectorized result. In the same sense, this vectorized “dot” operator can also be used with other binary operators.
Following example shows the use of “dot” operator −
julia> [1, 2, 3].^2
3-element Array{Int64,1}:
 1
 4
 9

Following table shows the numeric comparison operators that are supported on Julia’s primitive numeric types −
Following example shows the use of numeric comparison operators −
julia> 100 == 100
true

julia> 100 == 101
false

julia> 100 != 101
true

julia> 100 == 100.0
true

julia> 100 < 500
true

julia> 100 > 500
false

julia> 100 >= 100.0
true

julia> -100 <= 100
true

julia> -100 <= -100
true

julia> -100 <= -500
false

julia> 100 < -10.0
false

In Julia, the comparisons can be arbitrarily chained. In case of numerical code, the chaining comparisons are quite convenient. The && operator for scalar comparisons and  &  operator for elementwise comparison allows chained comparisons to work fine on arrays.
Following example shows the use of chained comparison −
julia> 100 < 200 <= 200 < 300 == 300 > 200 >= 100 == 100 < 300 != 500
true

In the following example, let us check out the evaluation behavior of chained comparisons −
julia> M(a) = (println(a); a)
M (generic function with 1 method)
julia> M(1) < M(2) <= M(3)
 2
 1
 3
true
julia> M(1) > M(2) <= M(3)
 2
 1
false

From highest precedence to lowest, the following table shows the order and associativity of operations applied by Julia −
We can also use Base.operator_precedence function to check the numerical precedence of a given operator. The example is given below −
julia> Base.operator_precedence(:-), Base.operator_precedence(:+), Base.operator_precedence(:.)
(11, 11, 17)


    73 Lectures 
     4 hours 

 Lemuel Ogbunude

    24 Lectures 
     3 hours 

 Mohammad Nauman

    29 Lectures 
     2.5 hours 

 Stone River ELearning
 Print
 Add Notes
 Bookmark this page