Skip to main content
The ~/ operator in Dart performs truncating division. It divides the left operand by the right operand and returns the integer portion of the quotient, discarding any fractional remainder by truncating towards zero.
int result = operand1 ~/ operand2;

Technical Characteristics

  • Operands: Both operands must be subtypes of num (int or double).
  • Return Type: The operation strictly evaluates to an int, regardless of the input operand types.
  • Precision: When both operands are int, ~/ performs true 64-bit integer division on the Dart VM. It is fundamentally different from (a / b).toInt(), which forces an intermediate conversion to double. The intermediate double conversion loses precision for integers requiring more than 53 bits, whereas ~/ maintains full 64-bit precision.

Evaluation Behavior

When evaluating the expression, Dart performs the division and strips the decimal value. It does not round to the nearest integer; it strictly truncates towards zero.
// Standard integer truncation
int a = 10 ~/ 3;     // Evaluates to 3
int b = -10 ~/ 3;    // Evaluates to -3 (truncates towards zero)

// Mixed and double operand truncation
int c = 10.5 ~/ 2;   // Evaluates to 5
int d = 9.9 ~/ 2.2;  // Evaluates to 4

// High-precision integer division (Dart VM)
// Maintains precision unlike (9000000000000000001 / 1).toInt()
int e = 9000000000000000001 ~/ 1; // Evaluates to 9000000000000000001

Exceptional States

Because the return type is strictly bound to int, the operator cannot return Infinity or NaN. Consequently, specific operand values will trigger runtime exceptions:
  • Division by Zero: Evaluating a ~/ 0 or a ~/ 0.0 strictly throws an UnsupportedError.
  • Non-finite Values: The operation throws an UnsupportedError if the left operand is double.infinity or double.negativeInfinity, or if either operand is double.nan. However, if the left operand is a finite number and the right operand is infinite (e.g., 5 ~/ double.infinity), the intermediate division evaluates to 0.0. Truncating 0.0 results in 0, so the operation successfully returns 0 without throwing an exception.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More