CKTIS / C++
Division operator /
The / operator calculates a quotient. For integer operands it discards the fractional part toward zero; a floating-point operand can change the calculation type. Convert before dividing to preserve a fractional result.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
a / bThe built-in operator a / b calculates a quotient. The usual arithmetic conversions establish a common operand and result type. When both converted operands are integers, the fractional part is discarded toward zero: 7 / 2 produces 3, and -7 / 2 produces -3.
With int and double operands, the int operand converts to double, so 7.0 / 2 produces 3.5. Writing static_cast<double>(a) / b changes the kind of division, whereas static_cast<double>(a / b) converts an already truncated result. Mixing signed and unsigned types can cause conversion to an unsigned type.
Integer division by zero and an integer quotient that is not representable in the result type cause undefined behavior. Representability is determined in the result type after the usual arithmetic conversions, which include integral promotions. For example, dividing the minimum int value by -1 has undefined behavior. In contrast, on a typical implementation, the minimum signed char value is promoted to int, and dividing it by -1 is well-defined because the quotient fits in int. Do not assume a portable result for floating-point division by zero; check the divisor.
These rules describe the built-in operator; an overloaded operator/ can have a different meaning. The supplied source is an evolving draft, not a frozen C++23 text. The example is intended for local verification with a C++23 flag.
Examples
#include <iostream>
int main() {
const int a = 7;
const int b = 2;
std::cout << a / b << '\n';
std::cout << -a / b << '\n';
std::cout << static_cast<double>(a) / b << '\n';
std::cout << static_cast<double>(a / b) << '\n';
}
Result
3 -3 3.5 3
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.