Math.floorMod(dividend, divisor) computes a floor-modulus result whose sign follows the divisor. With a positive divisor, the result is nonnegative, unlike Java’s remainder operator when the dividend is negative. section 4.4 section 4.5
Normalize a possibly negative angle
The remainder expression can produce an invalid negative result: int normalized = angle % 360;. Use int normalized = Math.floorMod(angle, 360); to map positive and negative angles into the 0–359 interval. section 4.5
Hash-code indexing
Replacing Math.abs(obj.hashCode()) % NUMBER_OF_NODES with Math.floorMod(obj.hashCode(), NUMBER_OF_NODES) avoids the Integer.MIN_VALUE failure and keeps a positive node count within range. section 4.4
Use Math.floorMod when:
- A dividend may be negative and the result must stay nonnegative for a positive divisor. section 4.5
- Avoiding Math.abs(... ) % divisor is important because Math.abs(Integer.MIN_VALUE) remains negative. section 4.4
- Do not use remainder-based random range generation; use Random.nextInt(bound) for a correct, uniform range. section 4.4
