processing icon indicating copy to clipboard operation
processing copied to clipboard

Modulo with negative number

Open mitchellpincham opened this issue 6 years ago • 2 comments

Modulo is not working with value1 as a negative number. println(-1 % 10); The result of this was -1. The expected result was 9.

mitchellpincham avatar Mar 28 '20 23:03 mitchellpincham

No, this is as expected.

% is performed by the underlying Java operator. In Java, % is calculated as the remainder, and "the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive."

See the language spec:

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3

If you want the other behavior, try using Math.floorMod(int x, int y) https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int-

Further discussion though should be on the discourse.processing.org forums; issues is for bug reports only.

jeremydouglass avatar Mar 29 '20 00:03 jeremydouglass

This issue has been automatically closed and locked.

Processing 3.x is no longer actively developed. Since January 2020, development moved to the Processing 4 repository.

If this issue is still relevant to Processing 4, we'd love to hear from you. Please open a new issue at: https://github.com/processing/processing4/issues/new/choose

Thank you for helping improve Processing.

github-actions[bot] avatar Jul 15 '26 15:07 github-actions[bot]