ZetCode

Julia Math Tutorial

last modified November 18, 2026

Julia's Base module provides many numeric functions that go beyond the basic arithmetic operators. This tutorial covers integer division, number theory helpers, combinatorics, floating-point precision, approximate comparison, rounding, and the safe computation of a hypotenuse.

A deterministic function produces the same result for the same input every time; all examples below use fixed inputs, so the printed output is stable. Each listing is a standalone main.jl file and was verified with Julia 1.13.4.

Integer Division

Integer division answers two questions at once: how many times one whole number fits into another, and what is left over. Julia has three related functions, and they differ in how they treat negative numbers.

main.jl
println(div(17, 5), " ", rem(17, 5), " ", mod(17, 5))
println(div(-17, 5), " ", rem(-17, 5), " ", mod(-17, 5))

div returns the quotient rounded toward zero, rem returns the remainder whose sign follows the dividend, and mod returns the remainder whose sign follows the divisor. For positive operands the last two agree; for -17 and 5 they differ: rem gives -2 while mod gives 3. The % operator seen in the operators tutorial is a synonym for rem.

$ julia main.jl
3 2 2
-3 -2 3

Division and Remainder Together

When a calculation needs both the quotient and the remainder, divrem computes them in one step.

main.jl
q, r = divrem(17, 5)
println("quotient: ", q, " remainder: ", r)
q2, r2 = divrem(-17, 5)
println("quotient: ", q2, " remainder: ", r2)

The function returns a tuple of two values, which are unpacked into q and r with destructuring. The remainder follows the same rule as rem, so a negative dividend produces a negative remainder.

$ julia main.jl
quotient: 3 remainder: 2
quotient: -3 remainder: -2

Greatest Common Divisor and Least Common Multiple

The greatest common divisor is the largest number that divides several integers without a remainder; the least common multiple is the smallest number that each of them divides.

main.jl
println(gcd(24, 36))
println(lcm(24, 36))
println(gcd(24, 36, 60))
println(lcm(4, 6, 9))

Twelve is the largest number dividing both 24 and 36, and 72 is the smallest number that both divide evenly. Both functions accept any number of arguments, as the three-argument calls show.

$ julia main.jl
12
72
12
36

Factorials

The factorial of a positive integer is the product of all whole numbers from 1 up to that integer. It grows extremely quickly.

main.jl
println(factorial(0))
println(factorial(5))
println(factorial(10))
println(factorial(big(30)))

By definition factorial(0) is 1. The result for 30 no longer fits in a fixed-size Int, so big(30) requests an arbitrary-precision integer. The big function and integer types are described in the data types tutorial.

$ julia main.jl
1
120
3628800
265252859812191058636308480000000

Binomial Coefficients

A binomial coefficient, written "n choose k", counts the number of ways to pick k items from a set of n items without regard to order.

main.jl
println(binomial(5, 2))
println(binomial(10, 3))
println(binomial(6, 0), " ", binomial(6, 6))

There are ten ways to choose two items from five, and 120 ways to choose three from ten. Choosing nothing or choosing everything gives exactly one combination. The coefficient can also be written as the factorial ratio factorial(n) / (factorial(k) * factorial(n - k)).

$ julia main.jl
10
120
1 1

Machine Epsilon

Floating-point numbers have finite precision. The eps function reports the gap between a number and the next representable number of the same type — a quantity often called machine epsilon.

main.jl
println(eps(Float64))
println(eps(1.0))
println(eps(0.0))
println(eps(Float32))

Called with a type, eps reports the spacing near 1.0 for that type: roughly 2.2e-16 for double precision and 1.2e-7 for single precision. Called with a value, it reports the spacing near that value; near zero this is the smallest positive subnormal number. The data types tutorial introduces eps in the context of floating-point types.

$ julia main.jl
2.220446049250313e-16
2.220446049250313e-16
5.0e-324
1.1920929e-7

Next and Previous Floating-Point Numbers

The functions nextfloat and prevfloat step to the immediately adjacent representable value.

main.jl
x = 1.0
println(prevfloat(x), " ", x, " ", nextfloat(x))
println(nextfloat(0.0))

The neighbour below 1.0 is 0.9999999999999999, while the one above is 1.0000000000000002 — not 1.0000000000000001 — because the spacing below a power of two is half the spacing above it. This is why exact equality is unreliable for floating-point results.

$ julia main.jl
0.9999999999999999 1.0 1.0000000000000002
5.0e-324

Approximate Comparison

Because of rounding, two calculations that should give the same number may differ in their last bits. The isapprox function compares values with a tolerance instead of requiring exact equality.

main.jl
println(isapprox(0.1 + 0.2, 0.3))
println(0.1 + 0.2 == 0.3)
println(isapprox(1.0, 1.0000001))
println(isapprox(1.0, 1.0000001; atol=1e-6))

Adding 0.1 and 0.2 does not produce exactly 0.3, so == is false, but the difference is tiny and isapprox accepts it. The default tolerance is relative, based on the square root of eps; a difference of one ten-millionth at 1.0 is too large by that measure. The atol keyword sets an absolute tolerance and makes the last comparison true.

$ julia main.jl
true
false
false
true

Rounding Functions

Four functions round a floating-point number, each with a different rule for what to do with the fractional part.

main.jl
x = 3.7
y = -3.7
println(floor(x), " ", ceil(x), " ", round(x), " ", trunc(x))
println(floor(y), " ", ceil(y), " ", round(y), " ", trunc(y))
println(floor(Int, x), " ", round(Int, x))
println(round(3.14159; digits=3))

floor rounds toward negative infinity, ceil toward positive infinity, trunc toward zero, and round to the nearest integer. Seen from negative values, the first three directions are clearly different. Without a type argument the results are floats; passing Int as the first argument returns integers. The digits keyword keeps a requested number of decimal places.

$ julia main.jl
3.0 4.0 4.0 3.0
-4.0 -3.0 -4.0 -3.0
3 4
3.142

The hypot Function

The length of a vector with components x and y is the square root of x² + y². The hypot function computes this directly and avoids overflowing while the squares are formed.

main.jl
println(hypot(3, 4))
println(hypot(5, 12))
println(sqrt(3^2 + 4^2))
println(hypot(1e200, 1e200))
println(sqrt(1e200^2 + 1e200^2))

The familiar 3-4-5 and 5-12-13 triangles come out as expected. With components of 1e200 the naive expression overflows to Inf, while hypot returns the correct finite length near 1.414e200. It also accepts more than two arguments, giving the length of a higher-dimensional vector.

$ julia main.jl
5.0
13.0
5.0
1.414213562373095e200
Inf

Best Practices for Numeric Code

A few habits make mathematical code easier to reason about.

Source

Julia Mathematical Operations documentation

Julia Numbers documentation

In this article we have worked with Julia's numeric functions: integer division, gcd and lcm, factorial and binomial, eps with its neighbouring values, isapprox, rounding, and hypot. The arithmetic operators are described in Julia operators, and the number types behind these functions are covered in Julia data types. Statistical summaries are the subject of Julia statistics.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Julia tutorials.