Arithmetic Operators
In short: Operators for the four basic arithmetic operations plus modulo (the remainder of a division) — +, -, *, /, %.
In more detail: The modulo operator (%) returns the remainder of an integer division and is often used, for example, to check whether a number is even (number % 2 == 0). When dividing two integers, the result is also cut off to an integer in many languages (no automatic rounding) — a common beginner mistake when a decimal result is actually expected.
In Depth
7 + 3 # 10
7 - 3 # 4
7 * 3 # 21
7 / 3 # 2.333... (Python: "true" division)
7 // 3 # 2 (Python: integer division, explicit)
7 % 3 # 1 (remainder of the division)Many languages (e.g. Java, C) distinguish / itself by data type: 7 / 3 gives 2 for two int operands (the decimal part is simply cut off, not rounded), while 7.0 / 3 as double division correctly gives 2.333.... This is one of the most common silent sources of error for beginners, because no error or warning occurs — the result is simply wrong when a decimal number was expected.
Modulo has even more practical applications than just checking even/odd:
index % array_length // "wrap-around": index always stays within the valid range (e.g. a ring buffer)
seconds % 60 // converting seconds -> minutes:seconds
number % 10 // extracting the last digit of a numberFor negative numbers, modulo behaves differently depending on the language — in some languages (Python), the result always has the same sign as the divisor, in others (Java, C) the same sign as the dividend. -7 % 3 gives 2 in Python, -1 in Java. Anyone calculating modulo with negative numbers should explicitly look this up for the language in use, rather than relying on universal behaviour.
Arithmetic operators follow the well-known multiplication-before-addition rule (see Operator Precedence) — *, /, and % bind more strongly than + and -.
Integer overflow
Integer data types have a fixed bit width and therefore a limited value range (see byte, long) — if you calculate beyond this limit, the value “overflows” and jumps (depending on the language) back to the smallest possible value, instead of throwing an error:
// 32-bit int, maximum value 2147483647
maxValue + 1 // gives -2147483648, NOT 2147483648 - a silent error!Some languages (Python) don’t have this problem at all, because integers there can automatically become arbitrarily large; others (Java, C, Rust in release mode) let the overflow pass through with no warning, which is one of the most classic, hardest-to-find error classes in older software — among other things, the cause of several well-known security vulnerabilities, because an overflowed value can, for example, unnoticeably bypass a length check.
Floating-point imprecision
For decimal numbers (see Floating-Point Number), the binary storage means that many decimal values that look “simple” at first glance can’t be represented exactly:
0.1 + 0.2 # gives 0.30000000000000004, not exactly 0.3This isn’t a bug of a specific language, but a property of the IEEE 754 floating-point format, which practically all modern languages use. For monetary amounts or other values where exact decimal precision counts, dedicated decimal types are therefore used (BigDecimal in Java, Decimal in Python/C#) instead of normal float/double values.
Operator overloading
In some languages (C++, Python, C#), you can define what +, -, etc. should mean for your own types (“operator overloading”) — e.g. so that two Vector objects can be added with v1 + v2, instead of having to call a method like v1.add(v2). This can make code read more intuitively, but quickly becomes confusing if misused (e.g. overloading + to do something completely unexpected) — most style guides recommend only overloading operators in a way that matches mathematical intuition.
See also: Operators, Operator Precedence