Integrated: 8301226: Add clamp() methods to java.lang.Math and to StrictMath

Tagir F. Valeev tvaleev at openjdk.org
Tue Feb 14 05:42:02 UTC 2023


On Sat, 4 Feb 2023 13:24:11 GMT, Tagir F. Valeev <tvaleev at 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.

This pull request has now been integrated.

Changeset: 94e7cc85
Author:    Tagir F. Valeev <tvaleev at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/94e7cc8587356988e713d23d1653bdd5c43fb3f1
Stats:     500 lines in 3 files changed: 499 ins; 0 del; 1 mod

8301226: Add clamp() methods to java.lang.Math and to StrictMath

Reviewed-by: qamai, darcy

-------------

PR: https://git.openjdk.org/jdk/pull/12428


More information about the core-libs-dev mailing list