- Julia Tutorial
- 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 - Discussion

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

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.

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 −

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 |

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 −

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 |

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 −

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)

Advertisements