~/ 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.
Technical Characteristics
- Operands: Both operands must be subtypes of
num(intordouble). - 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 todouble. The intermediatedoubleconversion 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.Exceptional States
Because the return type is strictly bound toint, the operator cannot return Infinity or NaN. Consequently, specific operand values will trigger runtime exceptions:
- Division by Zero: Evaluating
a ~/ 0ora ~/ 0.0strictly throws anUnsupportedError. - Non-finite Values: The operation throws an
UnsupportedErrorif the left operand isdouble.infinityordouble.negativeInfinity, or if either operand isdouble.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 to0.0. Truncating0.0results in0, so the operation successfully returns0without throwing an exception.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





