RFR: 8301226: Add clamp() methods to java.lang.Math and to StrictMath [v5]
Tagir F. Valeev
tvaleev at openjdk.org
Tue Feb 7 10:16:35 UTC 2023
> 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 two additional commits since the last revision:
- Tests refactoring
- More clarifying comments
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/12428/files
- new: https://git.openjdk.org/jdk/pull/12428/files/065018f4..b758b8db
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=12428&range=04
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=12428&range=03-04
Stats: 220 lines in 2 files changed: 84 ins; 36 del; 100 mod
Patch: https://git.openjdk.org/jdk/pull/12428.diff
Fetch: git fetch https://git.openjdk.org/jdk pull/12428/head:pull/12428
PR: https://git.openjdk.org/jdk/pull/12428
More information about the core-libs-dev
mailing list