
- Julia - Home
- Julia - Overview
- Julia - Environment Setup
- Julia - Basic Syntax
- Julia - Arrays
- Julia - Tuples
- Integers & Floating-Point Numbers
- Julia - Rational & Complex Numbers
- Julia - Basic Operators
- Basic Mathematical Functions
- Julia - Strings
- Julia - Functions
- Julia - Flow Control
- Julia - Dictionaries & Sets
- Julia - Date & Time
- Julia - Files I/O
- Julia - Metaprogramming
- Julia - Plotting
- Julia - Data Frames
- Working with Datasets
- Julia - Modules and Packages
- Working with Graphics
- Julia - Networking
- Julia - Databases
- Julia Useful Resources
- Julia - Quick Guide
- Julia - Useful Resources
- Julia - Cheatsheet
- Julia - Discussion
Julia - Basic Operators
In this chapter, we shall discuss different types of operators in Julia.
Arithmetic Operators
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 Julias primitive numeric types −
Expression | Name | Description |
---|---|---|
+x | unary plus | It is the identity operation. |
-x | unary minus | It maps values to their additive inverses. |
x + y | binary plus | It performs addition. |
x - y | binary minus | It performs subtraction. |
x * y | times | It performs multiplication. |
x / y | divide | It performs division. |
x y | integer divide | Denoted as x / y and truncated to an integer. |
x \ y | inverse divide | It is equivalent to y / x. |
x ^ y | power | It raises x to the yth power. |
x % y | remainder | It is equivalent to rem(x,y). |
!x | negation | It is negation on bool types and changes true to false and vice versa. |
The promotion system of Julia makes these arithmetic operations work naturally and automatically on the mixture of argument types.
Example
Following example shows the use of arithmetic operators −
julia> 2+20-517julia> 3-8-5julia> 50*2/1010.0julia> 23%21julia> 2^416
Bitwise Operators
Following table shows the bitwise operators that are supported on Julias primitive numeric types −
Sl.No | Expression Name | Name |
---|---|---|
1 | ∼x | bitwise not |
2 | x & y | bitwise and |
3 | x | y | bitwise or |
4 | x ⊻ y | bitwise xor (exclusive or) |
5 | x >>> y | logical shift right |
6 | x >> y | arithmetic shift right |
7 | x << y | logical/arithmetic shift left |
Example
Following example shows the use of bitwise operators −
julia> ∼1009-1010julia> 12&234julia> 12 & 234julia> 12 | 2331julia> 12 ⊻ 2327julia> xor(12, 23)27julia> ∼UInt32(12)0xfffffff3julia> ∼UInt8(12)0xf3
Updating Operators
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 −
+=
-=
*=
/=
\=
÷=
%=
^=
&=
|=
⊻=
>>>=
>>=
<<=
Example
Following example shows the use of updating operators −
julia> A = 100100julia> A +=100200julia> A200
Vectorized dot Operators
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.
Example
Following example shows the use of dot operator −
julia> [1, 2, 3].^23-element Array{Int64,1}: 1 4 9
Numeric Comparisons Operators
Following table shows the numeric comparison operators that are supported on Julias primitive numeric types −
Sl.No | Operator | Name |
---|---|---|
1 | == | Equality |
2 | !=,≠ | inequality |
3 | < | less than |
4 | <=, ≤ | less than or equal to |
5 | > | greater than |
6 | >=, ≥ | greater than or equal to |
Example
Following example shows the use of numeric comparison operators −
julia> 100 == 100truejulia> 100 == 101falsejulia> 100 != 101truejulia> 100 == 100.0truejulia> 100 < 500truejulia> 100 > 500falsejulia> 100 >= 100.0truejulia> -100 <= 100truejulia> -100 <= -100truejulia> -100 <= -500falsejulia> 100 < -10.0false
Chaining Comparisons
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.
Example
Following example shows the use of chained comparison −
julia> 100 < 200 <= 200 < 300 == 300 > 200 >= 100 == 100 < 300 != 500true
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 3truejulia> M(1) > M(2) <= M(3) 2 1false
Operator Precedence & Associativity
From highest precedence to lowest, the following table shows the order and associativity of operations applied by Julia −
Category | Operators | Associativity |
---|---|---|
Syntax | followed by :: | Left |
Exponentiation | ^ | Right |
Unary | + - | Right |
Bitshifts | << >> >>> | Left |
Fractions | // | Left |
Multiplication | * / % & \ ÷ | Left |
Addition | + - | ⊻ | Left |
Syntax | : .. | Left |
Syntax | |> | Left |
Syntax | <| | Right |
Comparisons | > < >= <= == === != !== <: | Non-associative |
Control flow | && followed by || followed by ? | Right |
Pair | => | Right |
Assignments | = += -= *= /= //= \= ^= = %= |= &= ⊻= <<= >>= >>>= | Right |
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)