Arithmetic operators should be familiar to you; however, we'll cover them in this section for consistency. The operators work as expected, and the succeeding table illustrates this using an example for each one. The remainder operator that returns the remainder from an integer division is worth noticing.
Let's look at an example to see this in more detail. First, we try to divide 10 by 2, and the remainder is 0 as expected:
> 10 % 2;; val it : int = 0
If we try to divide 10 by 3, we get 1 as the remainder, because 3 x 3 = 9, and 10 – 9 = 1:
> 10 % 3;; val it : int = 1
The following table shows arithmetic operators with examples and a description:
Operator |
Example |
Description |
---|---|---|
+ |
x + y |
Addition |
- |
x - y |
Subtraction |
* |
x * y |
Multiplication |
/ |
x / y |
Division |
% |
x % y |
Remainder |
- |
-x |
Unary minus |