Julia Basic Operators
An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation, e.g.: 3+2=5.
The Julia language has a rich set of built-in operators, supporting the following operations:
- Arithmetic operators
- Boolean operators
- Relational operators
- Bitwise operators
- Assignment operators
- Vectorized "dot" operators
Arithmetic operators
The following table shows Julia's basic arithmetic operators, applicable to all basic numeric types:
| Expression | Name | Description |
|---|---|---|
+x |
Unary plus operator | identity operation |
-x |
Unary minus operator | turn a value into its opposite |
x + y |
Binary addition operator | add two numbers |
x - y |
Binary subtraction operator | subtract two numbers |
x * y |
Multiplication operator | multiply two numbers |
x / y |
Division operator | divide two numbers, the result is always a floating-point number |
x ÷ y |
Integer division | take the integer part of x / y (rounded toward zero) |
x \ y |
Inverse division | is equivalent to y / x |
x ^ y |
Power operator | x 's y raised to a power |
x % y |
Remainder | is equivalent to rem(x,y) |
Example
julia
1
2
3
6
julia
1
2
1
julia
3
2
/
12
0.5
julia
2
20
5
17
julia
50
2
/
10
10.0
julia
23
%
2
1
julia
2
^
4
16
The integer-division symbol ÷ can be entered in the REPL with \div + Tab
julia
7
÷
2
3
Unlike most languages, Julia's integer division 1/0 does not raise an error but returns Inf
julia
1
/
0
Inf
Note:
2xThis notation of a number immediately followed by a variable also means multiplication (numeric literal coefficient), with higher precedence than ordinary multiplication/division, seeData Typesection.
Boolean operators
The following table shows Julia's boolean operators:
| Expression | Name |
|---|---|
!x |
Negation |
x && y |
Short-circuit AND; subexpression y is executed only when x is true |
x \|\| y |
Short-circuit OR; subexpression y is executed only when x is false |
Example
julia
!
true
false
julia
!
false
true
julia
true
&&
(
x =
(
1
,
2
,
3
)
)
(
1
,
2
,
3
)
julia
false
&&
(
x =
(
1
,
2
,
3
)
)
false
julia
false
||
(
x =
(
1
,
2
,
3
)
)
(
1
,
2
,
3
)
Relational operators
The following table shows Julia's relational operators:
| Operator | Name |
|---|---|
== |
Equality |
!=, ≠ |
Not equal |
< |
Less than |
<=, ≤ |
Less than or equal to |
> |
Greater than |
>=, ≥ |
Greater than or equal to |
Example
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
<= -
500
false
Chained comparisons
Julia allows chained comparisons, which is especially convenient when writing numerical code:
Example
julia
1
<
2
<
=
2
<
3
==
3
2
=
1
==
1
<
3!
=
5
true
Chained comparisons on arrays use .< and other dotted operators, yielding a boolean array
julia
A =
[
0.5
,
1.5
,
0.8
]
;
julia
0
.
<
A .
<
1
3-element BitVector:
1
0
1
Scalar chained comparisons internally use the && operator, while the array version uses bitwise &.
Note the evaluation order of chained comparisons: intermediate expressions are evaluated only once, but the evaluation order is undefined, so do not use expressions with side effects in chained comparisons:
Example
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
Bitwise operators
The following table shows Julia's bitwise operators:
| Expression | Name |
|---|---|
~x |
Bitwise NOT |
x & y |
Bitwise AND |
x \| y |
Bitwise OR |
x ⊻ y |
Bitwise XOR (also writable as xor(x,y)) |
x ⊼ y |
Bitwise NAND (also writable as nand(x,y)) |
x ⊽ y |
Bitwise NOR (also writable as nor(x,y)) |
x >>> y |
Logical right shift (fill left with 0) |
x >> y |
Arithmetic right shift (preserve the sign bit) |
x << y |
Left shift |
Example
julia
~
123
124
julia
123
&
234
106
julia
123
|
234
251
julia
123
⊻
234
145
julia
xor
(
123
,
234
)
145
julia
nand
(
123
,
123
)
124
julia
nor
(
123
,
124
)
128
julia
~UInt32
(
123
)
0xffffff84
julia
~UInt8
(
123
)
0x84
Assignment operators
Every binary arithmetic and bitwise operator can be combined with assignment to its left operand by placing = directly after the binary operator. For example, x += 3 is equivalent to x = x + 3.
The following table shows all of Julia's assignment operators:
| operator | Description | Example |
|---|---|---|
| = | Simple assignment operator, assigns the value of the right operand to the left operand | C = A + B will assign the value of A + B to C |
| += | Add-and-assign operator | C += A is equivalent to C = C + A |
| -= | Subtract-and-assign operator | C -= A is equivalent to C = C - A |
| *= | Multiply-and-assign operator | C *= A is equivalent to C = C * A |
| /= | Divide-and-assign operator | C /= A is equivalent to C = C / A |
| = | Inverse-divide-and-assign operator | C = A is equivalent to C = C \ A |
| ÷= | Integer-divide-and-assign operator | C ÷= A is equivalent to C = C ÷ A |
| %= | Remainder-and-assign operator | C %= A is equivalent to C = C % A |
| ^= | Power-and-assign operator | C ^= 2 is equivalent to C = C ^ 2 |
| //= | Rational-divide-and-assign operator | C //= A is equivalent to C = C // A |
| <<= | Left-shift-and-assign operator | C <<= 2 is equivalent to C = C << 2 |
| >>= | Arithmetic-right-shift-and-assign operator | C >>= 2 is equivalent to C = C >> 2 |
| >>>= | Logical-right-shift-and-assign operator | C >>>= 2 is equivalent to C = C >>> 2 |
| &= | Bitwise AND-and-assign operator | C &= 2 is equivalent to C = C & 2 |
| |= | Bitwise OR-and-assign operator | C |= 2 is equivalent to C = C | 2 |
| ⊻= | Bitwise XOR-and-assign operator | C ⊻= 2 is equivalent to C = C ⊻ 2 |
Note:In Julia
^is the power operator, not XOR, sox ^= 2means squaring; for bitwise XOR use⊻(or the xor function). This is exactly the opposite of the C language.
Example
julia
x =
1
1
julia
x +=
3
4
julia
x ^=
2
16
julia
x
16
Note:Update operators rebind the new value to the left-hand variable, so the variable's type may change. For example
x = 0x01(of type UInt8) executingx *= 2changes the type to Int64.
Vectorized "dot" operators
In Julia, every binary operator has a corresponding "dot" operator; for example ^ has a corresponding .^. This .^ is automatically defined by Julia to perform the ^ operation element-wise.
For example [1,2,3] ^ 3 is illegal because mathematics has not defined the cube of an array. But [1,2,3] .^ 3 is legal; it performs the ^ operation element-wise (also called vectorized operation):
Example
julia
[
1
,
2
,
3
]
.^
3
3
-element
Vector
{
Int64
}
:
1
8
27
Similarly, unary operators such as ! or √ also have a corresponding .√ for element-wise operation.
Dotted operations have one more important feature:fusing. Nested dotted operations are merged into a single loop, producing no temporary arrays, so performance is very good:
Example
julia
A =
[
1.0
,
2.0
,
3.0
]
;
The whole expression below loops only once, equivalent to computing 2a^2 + sin(a) for each element
julia
Y =
2
.
A.^
2
.+ sin.
(
A
)
3
-element
Vector
{
Float64
}
:
2.8414709848078967
8.909297426825682
18.141120008059865
The @. macro can add dots to all operations in an expression
julia
Y = @. 2A^
2
- sin
(
A
)
3
-element
Vector
{
Float64
}
:
2.8414709848078967
8.909297426825682
18.141120008059865
Besides dotted operators, we also have element-wise assignment operators; for example a .+= b (or @. a += b) is parsed as a .= a .+ b, meaning in-place modification of array a.
Note:Write
1 .+ xthe spaces on both sides of the dot are required,1.+xwould be parsed as1. + x(the floating-point number 1.0 plus x), causing an ambiguity error.
Operator precedence and associativity
Operator precedence determines how terms are grouped in an expression. For example x = 7 + 3 * 2; here x is assigned 13, not 20, because the operator * has higher precedence than +.
The following table lists operators from highest to lowest precedence:
| Classification | operator | Associativity |
|---|---|---|
| Field access | . |
Left-associative |
| Exponentiation | ^ |
Right-associative |
| Unary operators | + - ! ~ √ ∛ |
Right-associative |
| Shift operations | << >> >>> |
Left-associative |
| Fractions | // |
Left-associative |
| Multiplication | * / ÷ % & \ |
Left-associative |
| Addition | + - \| ⊻ |
Left-associative |
| Ranges and colons | : .. |
Left-associative |
| Pipeline | \|>, <\| |
Left-associative / right-associative |
| Comparison | > < >= <= == === != !== <: |
Chainable (no fixed associativity) |
| Flow control | && then \|\| |
Right-associative |
| Ternary conditional | ?: |
Right-associative |
| Pair operation | => |
Right-associative |
| Assignment | = += -= *= /= //= ^= ÷= %= \|= &= ⊻= <<= >>= >>>= |
Right-associative |
We can also use the built-in function Base.operator_precedence to view the precedence value of any given operator; the larger the value, the higher the precedence:
Example
julia
Base.
operator_precedence
(
:+
)
, Base.
operator_precedence
(
:
)
, Base.
operator_precedence
(
:.
)
(
11
,
12
,
17
)
julia
Base.
operator_precedence
(
:sin
)
, Base.
operator_precedence
(
:+=
)
, Base.
operator_precedence
(
:
(
=
)
)
(Note: parentheses are required around the equal sign :(=))
(
0
,
1
,
1
)
In addition, the built-in function Base.operator_associativity returns a symbolic representation of an operator's associativity:
Example
julia
Base.
operator_associativity
(
:-
)
, Base.
operator_associativity
(
:+
)
, Base.
operator_associativity
(
:^
)
(
:left, :none, :right
)
julia
Base.
operator_associativity
(
:⊗
)
, Base.
operator_associativity
(
:sin
)
, Base.
operator_associativity
(
:→
)
(
:left, :none, :right
)