Julia provides arithmetic operators for addition, subtraction, multiplication, division, integer division, exponentiation, and remainder calculations. These operators work with integers, floating-point numbers, complex numbers, and other numeric types supported by Julia.
This tutorial explains the Julia arithmetic operators, their syntax, return values, behavior with negative numbers, compound assignment forms, and element-wise arithmetic on arrays.
Julia Arithmetic Operators List
The following arithmetic operators are supported on primitive numeric types in Julia:
| Expression | Name | Description |
|---|---|---|
+x | unary plus | the identity operation |
-x | unary minus | maps values to their additive inverses |
x + y | binary plus | performs addition |
x - y | binary minus | performs subtraction |
x * y | times | performs multiplication |
x / y | divide | performs division |
x ÷ y | integer divide | x / y, truncated to an integer |
x \ y | inverse divide | equivalent to y / x |
x ^ y | power | raises x to the yth power |
x % y | remainder | equivalent to rem(x,y) |
The division operators require particular attention. The / operator performs ordinary division, ÷ performs truncated integer division, and \ reverses the operands before dividing.
Unary Plus and Unary Minus in Julia
A unary operator acts on one value. Unary plus returns the value without changing it, while unary minus returns its additive inverse.
Unary Plus
julia> x = 10
10
julia> +x
10
Unary plus is primarily useful for expressing intent or maintaining symmetry with unary minus. It does not convert a negative value into a positive value.
Unary Minus
julia> x = 10
10
julia> -x
-10
Applying unary minus twice restores the original value. For example, -(-10) evaluates to 10.
Addition and Subtraction Operators in Julia
Binary Plus
julia> x = 10
10
julia> y = 15
15
julia> x + y
25
The binary + operator adds two operands. Julia can promote compatible numeric types when necessary, so adding an integer to a floating-point number produces a floating-point result.
integer_value = 10
float_value = 2.5
result = integer_value + float_value
println(result)
println(typeof(result))
12.5
Float64
Binary Minus
julia> x = 10
10
julia> y = 15
15
julia> x - y
-5
The binary - operator subtracts its right operand from its left operand. Operand order therefore affects the result: x - y is generally different from y - x.
Multiplication Operator in Julia
Multiplication
julia> x = 10
10
julia> y = 15
15
julia> x * y
150
The * operator multiplies numbers. Julia also uses multiplication for supported mathematical objects such as matrices. Matrix multiplication and element-wise array multiplication are different operations, as explained later in this tutorial.
Division, Integer Division, and Inverse Division
Division
julia> x = 10
10
julia> y = 15
15
julia> x / y
0.6666666666666666
The / operator performs division. Dividing two integers with / normally produces a floating-point result rather than discarding the fractional part.
println(7 / 2)
println(typeof(7 / 2))
3.5
Float64
Integer Division
julia> x = 10
10
julia> y = 3
3
julia> x ÷ y
3
The ÷ operator performs truncated division. It removes the fractional part by rounding the quotient toward zero. The same operation can be written with the div function.
println(10 ÷ 3)
println(div(10, 3))
println(-10 ÷ 3)
3
3
-3
Because integer division truncates toward zero, -10 ÷ 3 evaluates to -3, not -4.
Inverse Division
julia> x = 10
10
julia> y = 3
3
julia> x \ y
0.3
For ordinary numbers, x \ y is equivalent to y / x. Therefore, 10 \ 3 evaluates to 3 / 10, which is 0.3.
The backslash operator is also used for left division in linear algebra. For example, when A is a matrix and b is a vector, A \ b can be used to solve a linear system without explicitly calculating a matrix inverse.
Exponentiation with the Julia Power Operator
Power
julia> x = 11
11
julia> y = 4
4
julia> x ^ y
14641
The ^ operator raises its left operand to the power of its right operand. For example, 2 ^ 5 means 2 multiplied by itself five times.
println(2 ^ 5)
println(9 ^ 0.5)
println(2.0 ^ -3)
32
3.0
0.125
Use parentheses when combining a negative literal with exponentiation. The expressions -2^2 and (-2)^2 do not mean the same thing: the first applies unary minus after exponentiation, while the second squares the negative value.
println(-2^2)
println((-2)^2)
-4
4
Remainder and Modulo Calculations in Julia
Remainder
julia> x = 11
11
julia> y = 4
4
julia> x % y
3
The % operator returns the remainder after division and is equivalent to calling rem(x, y). It is commonly used to test divisibility and distinguish even numbers from odd numbers.
number = 18
if number % 2 == 0
println("The number is even")
else
println("The number is odd")
end
The number is even
Julia also provides the mod function. For positive operands, rem and mod often produce the same result. They can differ when negative values are involved because rem follows truncated division, while mod follows floored division.
println(rem(-11, 4))
println(mod(-11, 4))
-3
1
Julia Arithmetic Assignment Operators
Arithmetic assignment operators combine a calculation with assignment. They provide a shorter way to update a variable using its current value.
| Assignment | Equivalent expression |
|---|---|
x += y | x = x + y |
x -= y | x = x - y |
x *= y | x = x * y |
x /= y | x = x / y |
x ÷= y | x = x ÷ y |
x \= y | x = x \ y |
x ^= y | x = x ^ y |
x %= y | x = x % y |
total = 10
total += 5
println(total)
total *= 2
println(total)
total -= 4
println(total)
15
30
26
An updating operator assigns a new result to the variable. The result’s type can differ from the original variable’s type. For example, applying /= to an integer may produce a floating-point value.
Element-Wise Arithmetic Operators for Julia Arrays
Julia uses dot syntax to apply many operators element by element. Examples include .+, .-, .*, ./, and .^. This behavior is useful when the same arithmetic operation must be performed on every value in an array.
values = [1, 2, 3, 4]
println(values .+ 10)
println(values .* 2)
println(values .^ 2)
[11, 12, 13, 14]
[2, 4, 6, 8]
[1, 4, 9, 16]
The distinction between * and .* is especially important for arrays. The * operator can represent matrix multiplication, whereas .* multiplies corresponding elements.
a = [1, 2, 3]
b = [4, 5, 6]
println(a .* b)
[4, 10, 18]
Absolute Values and Related Numeric Functions
Absolute value is obtained with the abs function rather than a dedicated arithmetic symbol. The result is the magnitude of the value without its sign.
println(abs(-15))
println(abs(3.5))
println(abs(3 + 4im))
15
3.5
5.0
Other useful functions for arithmetic calculations include sqrt for square roots, div for truncated division, rem for remainders, and mod for modulo results.
Arithmetic Operator Precedence in Julia
When an expression contains several operators, Julia evaluates them according to operator precedence. Exponentiation is evaluated before multiplication and division, which are evaluated before addition and subtraction.
result_without_parentheses = 2 + 3 * 4
result_with_parentheses = (2 + 3) * 4
println(result_without_parentheses)
println(result_with_parentheses)
14
20
Use parentheses when the intended order is not immediately clear. Parentheses make arithmetic expressions easier to review and reduce mistakes caused by an incorrect assumption about precedence.
Common Mistakes with Julia Arithmetic Operators
- Using
÷when a fractional result is required. Use/for ordinary division. - Assuming
%andmodalways behave identically with negative values. - Writing
-2^2when the intended expression is(-2)^2. - Using
*instead of.*for element-wise multiplication of arrays. - Dividing by zero without considering the numeric types and behavior required by the program.
- Assuming an updating assignment such as
x /= 2must preserve the original type ofx.
Julia Arithmetic Operators FAQ
What is the difference between / and ÷ in Julia?
The / operator performs ordinary division and can return a fractional value. The ÷ operator performs truncated division and removes the fractional part by rounding the quotient toward zero.
How do I calculate a remainder in Julia?
Use x % y or rem(x, y) to calculate a remainder. For modulo behavior based on floored division, use mod(x, y).
How do I calculate an exponent in Julia?
Use the ^ operator. For example, 2 ^ 4 evaluates to 16. Parenthesize a negative base, as in (-2)^4.
How do I perform element-wise arithmetic on a Julia array?
Place a dot before the operator, such as .+, .*, ./, or .^. For example, [1, 2, 3] .* 2 produces [2, 4, 6].
How do I find the absolute value of a number in Julia?
Call the abs function. For example, abs(-8) returns 8.
Editorial QA Checklist for Julia Arithmetic Operators
- Confirm that
/,÷, and\are described as distinct division operations. - Verify that negative-number examples distinguish
remfrommod. - Check that exponentiation examples use parentheses when the base is negative.
- Ensure that array examples distinguish ordinary operators from dotted element-wise operators.
- Run added Julia examples and confirm that the displayed output matches the code.
Conclusion
In this Julia Tutorial, we learned how to use Julia arithmetic operators for unary operations, addition, subtraction, multiplication, division, integer division, inverse division, exponentiation, and remainder calculations. We also covered arithmetic assignment operators, operator precedence, absolute values, negative-number behavior, and element-wise operations on arrays.
TutorialKart.com