ZetCode

F# operators

last modified December 2, 2026

In this article we cover the operators of F#: arithmetic, comparison, Boolean, string and bitwise operators, and the rules that decide in which order they are applied.

An operator is a symbol that computes a value from one or two operands. Most F# operators are infix and are written between the operands, as in a + b; a few, like the unary minus, are prefix. Both operands of an arithmetic operator must have the same type; F# does not mix, for example, an int and a float in one operation. The types themselves are described in F# data types.

Arithmetic operators

The arithmetic operators are +, -, *, / and %. On integers, division drops the fractional part, and % returns the remainder.

arithmetic.fsx
let a = 17
let b = 5

printfn "%d + %d = %d" a b (a + b)
printfn "%d - %d = %d" a b (a - b)
printfn "%d * %d = %d" a b (a * b)
printfn "%d / %d = %d" a b (a / b)
printfn "%d %% %d = %d" a b (a % b)
printfn "-%d %% %d = %d" a b (-a % b)

We apply the five operators to two integers.

printfn "%d %% %d = %d" a b (a % b)

A percent sign has a special meaning in a format string, so a literal % is written as %%.

printfn "-%d %% %d = %d" a b (-a % b)

The remainder has the sign of the left operand: -17 % 5 is -2, not 3. The minus before a is the unary negation operator.

λ dotnet fsi arithmetic.fsx
17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3
17 % 5 = 2
-17 % 5 = -2

Floating-point arithmetic and powers

On floats, division keeps the fraction. The ** operator raises a float to a power, and the pown function raises a number of any numeric type to an integer power.

floats.fsx
let x = 17.0
let y = 5.0

printfn "%.1f / %.1f = %.2f" x y (x / y)
printfn "%.1f %% %.1f = %.1f" x y (x % y)
printfn "2.0 ** 10.0 = %.1f" (2.0 ** 10.0)
printfn "sqrt 2.0 = %.4f" (sqrt 2.0)

printfn "pown 3 4 = %d" (pown 3 4)
printfn "pown 1.5 2 = %.2f" (pown 1.5 2)

let n = 7
printfn "%d / 2 = %d" n (n / 2)
printfn "%d / 2.0 = %.1f" n (float n / 2.0)

The program divides floats, computes powers and a square root, and shows how to get a fractional result from integers.

printfn "2.0 ** 10.0 = %.1f" (2.0 ** 10.0)
printfn "sqrt 2.0 = %.4f" (sqrt 2.0)

The ** operator works only with floating-point numbers; 2 ** 10 is a compile error. sqrt is a built-in function.

printfn "pown 3 4 = %d" (pown 3 4)

pown takes a base of any numeric type and an integer exponent, so it computes integer powers.

printfn "%d / 2.0 = %.1f" n (float n / 2.0)

To divide an integer and keep the fraction, we convert it to a float first.

λ dotnet fsi floats.fsx
17.0 / 5.0 = 3.40
17.0 % 5.0 = 2.0
2.0 ** 10.0 = 1024.0
sqrt 2.0 = 1.4142
pown 3 4 = 81
pown 1.5 2 = 2.25
7 / 2 = 3
7 / 2.0 = 3.5

Precedence and associativity

When an expression has several operators, precedence decides which is applied first, and associativity decides the order of operators with the same precedence. Parentheses override both.

precedence.fsx
printfn "%d" (3 + 4 * 2)
printfn "%d" ((3 + 4) * 2)

printfn "%d" (20 - 8 - 2)
printfn "%d" (20 - (8 - 2))

printfn "%.1f" (2.0 ** 3.0 ** 2.0)
printfn "%.1f" ((2.0 ** 3.0) ** 2.0)

printfn "%d" (-2 * 3 + 10 % 4)
printfn "%b" (1 + 2 < 2 * 2 && 5 > 4)

Each pair of lines computes an expression without and with parentheses.

printfn "%d" (3 + 4 * 2)
printfn "%d" ((3 + 4) * 2)

Multiplication has a higher precedence than addition, so the first expression is 3 + 8.

printfn "%d" (20 - 8 - 2)
printfn "%d" (20 - (8 - 2))

Subtraction is left-associative: 20 - 8 - 2 means (20 - 8) - 2.

printfn "%.1f" (2.0 ** 3.0 ** 2.0)

The power operator is right-associative, so the expression is 2.0 ** 9.0, which is 512.

printfn "%b" (1 + 2 < 2 * 2 && 5 > 4)

Arithmetic binds more tightly than comparison, and comparison more tightly than the Boolean operators. From the highest precedence to the lowest, the main groups are: function application, unary minus, **, * / %, + -, comparisons, &&, and ||.

λ dotnet fsi precedence.fsx
11
14
10
14
512.0
64.0
-4
true

Comparison operators

The comparison operators are =, <>, <, >, <= and >=; they return a bool. Note that equality is a single = and inequality is <>, not == and != as in C-like languages.

comparison.fsx
let a = 4
let b = 7

printfn "%d = %d: %b" a b (a = b)
printfn "%d <> %d: %b" a b (a <> b)
printfn "%d < %d: %b" a b (a < b)
printfn "%d >= %d: %b" a b (a >= b)

printfn "\"apple\" < \"banana\": %b" ("apple" < "banana")
printfn "'a' < 'B': %b" ('a' < 'B')
printfn "true > false: %b" (true > false)

printfn "compare 4 7 = %d" (compare a b)
printfn "compare 7 4 = %d" (compare b a)
printfn "max 4 7 = %d, min 4 7 = %d" (max a b) (min a b)

We compare numbers, strings, characters and Booleans.

printfn "\"apple\" < \"banana\": %b" ("apple" < "banana")
printfn "'a' < 'B': %b" ('a' < 'B')

Strings are compared character by character, and characters by their codes. Lowercase 'a' has the code 97 and uppercase 'B' the code 66, so 'a' is greater. The %b placeholder prints a Boolean.

printfn "compare 4 7 = %d" (compare a b)

The compare function returns a negative number when the first value is smaller, zero when they are equal, and a positive number otherwise. The max and min functions return the larger and the smaller value.

λ dotnet fsi comparison.fsx
4 = 7: false
4 <> 7: true
4 < 7: true
4 >= 7: false
"apple" < "banana": true
'a' < 'B': false
true > false: true
compare 4 7 = -1
compare 7 4 = 1
max 4 7 = 7, min 4 7 = 4

Boolean operators

The Boolean operators are && (and), || (or), and the not function.

boolean.fsx
let age = 25
let hasTicket = true

let canEnter = age >= 18 && hasTicket
let discount = age < 26 || age >= 65
let noTicket = not hasTicket

printfn "can enter: %b" canEnter
printfn "discount: %b" discount
printfn "no ticket: %b" noTicket

let divisor = 0
let check = divisor <> 0 && 100 / divisor > 5

printfn "check: %b" check

We combine conditions about a visitor.

let canEnter = age >= 18 && hasTicket
let discount = age < 26 || age >= 65
let noTicket = not hasTicket

not is an ordinary function, so it is written before its argument like any other function call.

let divisor = 0
let check = divisor <> 0 && 100 / divisor > 5

The && and || operators are short-circuiting: the right operand is evaluated only when the left one does not decide the result. Here the left operand is false, so the division by zero is never performed.

λ dotnet fsi boolean.fsx
can enter: true
discount: true
no ticket: false
check: false

String operators

The + operator joins strings, and the comparison operators work on strings as well.

strings.fsx
let first = "Lucia"
let last = "Novak"

let full = first + " " + last
printfn "%s" full

let line = "-" + "=" + "-"
printfn "%s" line

printfn "%b" (first = "Lucia")
printfn "%b" ("Lucia" < "lucia")
printfn "%d" (compare "Lucia" "Lucas")

let total = 3 + 4
printfn "%s" ("total: " + string total)

We build and compare strings.

printfn "%b" ("Lucia" < "lucia")
printfn "%d" (compare "Lucia" "Lucas")

F# compares strings ordinally, by the codes of their characters. Uppercase letters have smaller codes than lowercase ones, so "Lucia" is less than "lucia". In the second comparison the strings differ at the fourth character; compare returns the difference between 'i' and 'a'. Only the sign of the result has a meaning.

printfn "%s" ("total: " + string total)

The + operator does not convert numbers to strings; we use the string function.

λ dotnet fsi strings.fsx
Lucia Novak
-=-
true
true
8
total: 7

Bitwise operators

The bitwise operators work on the bits of integers. F# writes them with three characters: &&& (and), ||| (or), ^^^ (exclusive or), ~~~ (not), <<< (shift left), and >>> (shift right).

bitwise.fsx
let a = 0b1100
let b = 0b1010

printfn "a       = %04B" a
printfn "b       = %04B" b
printfn "a &&& b = %04B" (a &&& b)
printfn "a ||| b = %04B" (a ||| b)
printfn "a ^^^ b = %04B" (a ^^^ b)

printfn "1 <<< 4 = %d" (1 <<< 4)
printfn "96 >>> 3 = %d" (96 >>> 3)
printfn "~~~0 = %d" (~~~0)

let flags = 0b0101
let mask = 0b0100
printfn "bit 2 set: %b" ((flags &&& mask) <> 0)

We apply the bitwise operators to two four-bit numbers.

let a = 0b1100
let b = 0b1010

The 0b prefix starts a binary literal; 0x starts a hexadecimal one. The %04B placeholder prints an integer in binary, padded with zeros to four digits.

printfn "1 <<< 4 = %d" (1 <<< 4)
printfn "96 >>> 3 = %d" (96 >>> 3)

Shifting left by n bits multiplies by 2n, and shifting right divides by it.

let flags = 0b0101
let mask = 0b0100
printfn "bit 2 set: %b" ((flags &&& mask) <> 0)

A common use of bitwise and is testing whether a bit is set in a set of flags.

λ dotnet fsi bitwise.fsx
a       = 1100
b       = 1010
a &&& b = 1000
a ||| b = 1110
a ^^^ b = 0110
1 <<< 4 = 16
96 >>> 3 = 12
~~~0 = -1
bit 2 set: true

Source

F# language reference: Symbol and operator reference

F# language reference: Arithmetic operators

F# language reference: Bitwise operators

In this article we have covered the F# arithmetic, comparison, Boolean, string and bitwise operators and their precedence. Comparisons are used in F# conditionals; the pipe and composition operators are covered in F# pipelines and composition, which also shows how to define our own operators.

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 F# tutorials.