% operator calculates the Euclidean modulo of two numeric operands. Unlike the remainder operator found in languages such as C or Java (which typically perform truncated division), Dart’s % operator guarantees a non-negative result for all finite inputs, regardless of the signs of the dividend or divisor.
Syntax
Return Type
The static type of the result is determined by the operand types:int: Returned if both operands are of typeint.double: Returned if at least one operand is of typedouble.
Operational Logic
The operator adheres to the mathematical definition of Euclidean division. For an expressiona % b, the result r satisfies the following invariant:
0 <= r < |b|(The result is non-negative and strictly less than the absolute value of the divisor).a = b * q + rfor some integerq.
remainder() method, which performs truncated division where the result adopts the sign of the dividend.
Integer Behavior
The operator resolves to a non-negative integer even when the dividend is negative.Floating-Point Behavior
The operator supports floating-point operands, returning the floating-point remainder of the Euclidean division.Edge Cases
- Integer Division by Zero (VM): If both operands are integers and the divisor is
0, the Dart VM throws anIntegerDivisionByZeroException. - Integer Division by Zero (Web): When compiled to JavaScript, integers are represented as floating-point numbers. Consequently, division by zero results in
NaNrather than throwing an exception. - Floating-Point Division by Zero: If any operand is a
doubleand the divisor is0or0.0, the result isNaN.
Master Dart with Deep Grasping Methodology!Learn More





