TExcelTAD
Home › Julia › Julia Math Operations

Julia Math Operations

Julia's arithmetic operators and mathematical functions are designed for scientific work: they cover integer and floating-point division, powers, modular arithmetic and a large library of elementary functions.

Arithmetic operators

Operator Name Example Result
+ - * add, subtract, multiply 7 * 3 21
/ division (always floating point) 7 / 2 3.5
÷ or div integer division (truncates) 7 ÷ 2 3
% or rem remainder 7 % 3 1
^ power 2 ^ 10 1024
\ inverse divide (a \ b == b / a) 2 \ 8 4.0
julia · try it yourself
println(7 / 2)
println(7 ÷ 2)
println(div(-7, 2), " ", fld(-7, 2))   # truncate vs floor
println(-7 % 3, " ", mod(-7, 3))       # rem keeps the sign of the dividend, mod of the divisor
println(2^10, " ", 2.0^-1)
println(divrem(17, 5))
3.5
3
-3 -4
-1 2
1024 0.5
(3, 2)

Note: / on two integers always returns a float: 6 / 3 is 2.0. Use ÷ (type \div + Tab) when you want an integer result.

Operator precedence

Precedence follows normal maths: ^ binds tightest, then unary minus, then * / ÷ %, then + -, then comparisons. Powers associate from the right, and -2^2 is -(2^2).

julia · try it yourself
println(2 + 3 * 4)
println((2 + 3) * 4)
println(2^3^2)        # 2^(3^2) = 2^9
println(-2^2)
x = 3
println(2x^2 + 1)     # juxtaposition: 2*(x^2) + 1
14
20
512
-4
19

Updating operators

Every binary arithmetic operator has an updating form: x += 1 is shorthand for x = x + 1.

julia · try it yourself
score = 10
score += 5
score *= 2
score -= 4
score ÷= 3
println(score)
8

Mathematical functions

Category Functions
Rounding round, floor, ceil, trunc
Roots & powers sqrt (√), cbrt, exp, log, log2, log10, hypot
Trigonometry sin, cos, tan, asin, atan, sind (degrees), deg2rad
Sign & magnitude abs, sign, copysign, min, max, clamp
Integers gcd, lcm, factorial, binomial, isodd, iseven, isprime is not in Base
julia · try it yourself
println(sqrt(16), " ", √2)
println(round(π, digits=4), " ", round(2.5), " ", round(3.5))
println(floor(-2.5), " ", ceil(-2.5))
println(round(exp(1), digits=5), " ", log(exp(2)), " ", log10(1000), " ", log2(8))
println(sind(30), " ", round(cos(π), digits=2))
println(abs(-4), " ", hypot(3, 4), " ", clamp(15, 0, 10))
println(gcd(84, 36), " ", lcm(4, 6), " ", binomial(5, 2))
4.0 1.4142135623730951
3.1416 2.0 4.0
-3.0 -2.0
2.71828 2.0 3.0 3.0
0.5 -1.0
4 5.0 10
12 12 10

round uses "round half to even" (banker's rounding) by default, so round(2.5) is 2.0 and round(3.5) is 4.0. Pass RoundNearestTiesAway as a second argument for school-style rounding.

Bitwise operators

Operator Meaning
&, |, ⊻ (xor) and, or, exclusive or
~ bitwise not
<<, >>, >>> shifts (arithmetic and logical right)
julia · try it yourself
a, b = 0b1100, 0b1010
println(a & b, " ", a | b, " ", a ⊻ b)
println(1 << 4, " ", 256 >> 2)
println(bitstring(UInt8(13)))
println(count_ones(255))
8 14 6
16 64
00001101
8

Test yourself

What is 7 ÷ 2 in Julia?

  1. 3.5
  2. 3
  3. 4
  4. 1

What does mod(-7, 3) return?

  1. -1
  2. 1
  3. 2
  4. -2

What does round(2.5) return by default?

  1. 3.0
  2. 2.0
  3. 2
  4. 3