On Sat, 4 Feb 2023 21:34:50 GMT, Tagir F. Valeev <tval...@openjdk.org> wrote:
>> clamp() methods added to Math and StrictMath >> >> `int clamp(long, int, int)` is somewhat different, as it accepts a `long` >> value and safely clamps it to an `int` range. Other overloads work with a >> particular type (long, float and double). Using similar approach in other >> cases (e.g. `float clamp(double, float, float)`) may cause accidental >> precision loss even if the value is within range, so I decided to avoid this. >> >> In all cases, `max >= min` precondition should met. For double and float we >> additionally order `-0.0 < 0.0`, similarly to what Math.max or >> Double.compare do. In double and float overloads I try to keep at most one >> arg-check comparison on common path, so the order of checks might look >> unusual. >> >> For tests, I noticed that tests in java/lang/Math don't use any testing >> framework (even newer tests), so I somehow mimic the approach of neighbour >> tests. > > Tagir F. Valeev has updated the pull request incrementally with one > additional commit since the last revision: > > Typo in doc fixed src/java.base/share/classes/java/lang/Math.java line 2213: > 2211: * @since 21 > 2212: */ > 2213: public static int clamp(long value, int min, int max) { There should probably also be a pure `int` overload: public static int clamp(int value, int min, int max) src/java.base/share/classes/java/lang/Math.java line 2215: > 2213: public static int clamp(long value, int min, int max) { > 2214: if (min > max) { > 2215: throw new IllegalArgumentException(min + ">" + max); These should probably have some spacing or a better error message: Suggestion: throw new IllegalArgumentException(min + " > " + max); src/java.base/share/classes/java/lang/Math.java line 2236: > 2234: public static long clamp(long value, long min, long max) { > 2235: if (min > max) { > 2236: throw new IllegalArgumentException(min + ">" + max); Suggestion: throw new IllegalArgumentException(min + " > " + max); src/java.base/share/classes/java/lang/Math.java line 2270: > 2268: } > 2269: if (Double.compare(min, max) > 0) { > 2270: throw new IllegalArgumentException(min + ">" + max); Suggestion: throw new IllegalArgumentException(min + " > " + max); src/java.base/share/classes/java/lang/Math.java line 2305: > 2303: } > 2304: if (Float.compare(min, max) > 0) { > 2305: throw new IllegalArgumentException(min + ">" + max); Suggestion: throw new IllegalArgumentException(min + " > " + max); ------------- PR: https://git.openjdk.org/jdk/pull/12428