This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-rng.git
commit 1197da86b4955be0fbb7900702feda8fccfdb7dc Author: Alex Herbert <[email protected]> AuthorDate: Thu Sep 17 10:22:24 2026 +0100 Checkstyle: Remove unnecessary parentheses --- .../java/org/apache/commons/rng/core/source32/Philox4x32.java | 8 +++++--- .../java/org/apache/commons/rng/core/source64/Philox4x64.java | 8 +++++--- .../java/org/apache/commons/rng/core/util/NumberFactory.java | 6 +++--- .../org/apache/commons/rng/core/util/NumberFactoryTest.java | 2 +- .../rng/sampling/distribution/AliasMethodDiscreteSampler.java | 2 +- .../distribution/ZigguratNormalizedGaussianSampler.java | 2 +- .../commons/rng/sampling/distribution/ZigguratSampler.java | 10 +++++----- .../commons/rng/sampling/distribution/StableSamplerTest.java | 2 +- 8 files changed, 22 insertions(+), 18 deletions(-) diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/Philox4x32.java b/commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/Philox4x32.java index 61c0ab07..b4faa2a2 100644 --- a/commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/Philox4x32.java +++ b/commons-rng-core/src/main/java/org/apache/commons/rng/core/source32/Philox4x32.java @@ -334,13 +334,15 @@ public final class Philox4x32 extends IntProvider implements LongJumpableUniform /** * Gets the buffer position increment from the jump distance. * - * @param distance Jump distance. + * @param distance Jump distance (must be positive). * @return the buffer position increment */ private static int getBufferPositionIncrement(double distance) { return distance < TWO_POW_54 ? - // 2 least significant digits from the integer representation - (int)((long) distance) & 0x3 : + // 2 least significant digits from the integer representation. + // The cast is to long to obtain a 53-bit integer, then to int for the + // return type which is masked at 2 bits. + (int) (long) distance & 0x3 : 0; } diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/core/source64/Philox4x64.java b/commons-rng-core/src/main/java/org/apache/commons/rng/core/source64/Philox4x64.java index 8e92925d..a5089459 100644 --- a/commons-rng-core/src/main/java/org/apache/commons/rng/core/source64/Philox4x64.java +++ b/commons-rng-core/src/main/java/org/apache/commons/rng/core/source64/Philox4x64.java @@ -329,13 +329,15 @@ public final class Philox4x64 extends LongProvider implements LongJumpableUnifor /** * Gets the buffer position increment from the jump distance. * - * @param distance Jump distance. + * @param distance Jump distance (must be positive). * @return the buffer position increment */ private static int getBufferPositionIncrement(double distance) { return distance < TWO_POW_54 ? - // 2 least significant digits from the integer representation - (int)((long) distance) & 0x3 : + // 2 least significant digits from the integer representation. + // The cast is to long to obtain a 53-bit integer, then to int for the + // return type which is masked at 2 bits. + (int) (long) distance & 0x3 : 0; } diff --git a/commons-rng-core/src/main/java/org/apache/commons/rng/core/util/NumberFactory.java b/commons-rng-core/src/main/java/org/apache/commons/rng/core/util/NumberFactory.java index 94640615..a9f3d2f4 100644 --- a/commons-rng-core/src/main/java/org/apache/commons/rng/core/util/NumberFactory.java +++ b/commons-rng-core/src/main/java/org/apache/commons/rng/core/util/NumberFactory.java @@ -99,8 +99,8 @@ public final class NumberFactory { int w) { // Require the least significant 53-bits from a long. // Join the most significant 26 from v with 27 from w. - final long high = ((long) (v >>> 6)) << 27; // 26-bits remain - final int low = w >>> 5; // 27-bits remain + final long high = (long) (v >>> 6) << 27; // 26-bits remain + final int low = w >>> 5; // 27-bits remain return (high | low) * DOUBLE_MULTIPLIER; } @@ -124,7 +124,7 @@ public final class NumberFactory { */ public static long makeLong(int v, int w) { - return (((long) v) << 32) | (w & 0xffffffffL); + return (long) v << 32 | (w & 0xffffffffL); } /** diff --git a/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java b/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java index 961fcee4..7953ca50 100644 --- a/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java +++ b/commons-rng-core/src/test/java/org/apache/commons/rng/core/util/NumberFactoryTest.java @@ -101,7 +101,7 @@ class NumberFactoryTest { final int vL = NumberFactory.extractLo(v); final int vH = NumberFactory.extractHi(v); - final long actual = (((long) vH) << 32) | (vL & 0xffffffffL); + final long actual = (long) vH << 32 | (vL & 0xffffffffL); Assertions.assertEquals(v, actual); } diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AliasMethodDiscreteSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AliasMethodDiscreteSampler.java index 6529321c..b8f13ee9 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AliasMethodDiscreteSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AliasMethodDiscreteSampler.java @@ -213,7 +213,7 @@ public class AliasMethodDiscreteSampler // Create a uniform random deviate as a long. // This replicates functionality from the o.a.c.rng.core.utils.NumberFactory.makeLong - final long longBits = (((long) rng.nextInt()) << 32) | (bits & 0xffffffffL); + final long longBits = (long) rng.nextInt() << 32 | (bits & 0xffffffffL); // Choose between the two. Use a 53-bit long for the probability. return (longBits >>> 11) < probability[j] ? j : alias[j]; diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.java index 8b378189..998adcf0 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratNormalizedGaussianSampler.java @@ -107,7 +107,7 @@ public class ZigguratNormalizedGaussianSampler @Override public double sample() { final long j = rng.nextLong(); - final int i = ((int) j) & LAST; + final int i = (int) j & LAST; if (Math.abs(j) < K[i]) { // This branch is called about 0.985086 times per sample. return j * W[i]; diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratSampler.java index e48d2c0d..363e32f0 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/ZigguratSampler.java @@ -582,7 +582,7 @@ public abstract class ZigguratSampler implements SharedStateContinuousSampler { final long x = nextLong(); // Float multiplication squashes these last 8 bits, so they can be used to sample i - final int i = ((int) x) & MASK_INT8; + final int i = (int) x & MASK_INT8; if (i < I_MAX) { // Early exit. @@ -626,7 +626,7 @@ public abstract class ZigguratSampler implements SharedStateContinuousSampler { for (;;) { // Duplicate of the sample() method final long x = nextLong(); - final int i = ((int) x) & 0xff; + final int i = (int) x & 0xff; if (i < I_MAX) { // Early exit. @@ -652,7 +652,7 @@ public abstract class ZigguratSampler implements SharedStateContinuousSampler { private int selectRegion() { final long x = nextLong(); // j in [0, 256) - final int j = ((int) x) & MASK_INT8; + final int j = (int) x & MASK_INT8; // map to j in [0, N] with N the number of layers of the ziggurat return x >= IPMF[j] ? MAP[j] & MASK_INT8 : j; } @@ -1062,7 +1062,7 @@ public abstract class ZigguratSampler implements SharedStateContinuousSampler { // (which defaults to 35 bytes). This compiles to 33 bytes. final long xx = nextLong(); // Float multiplication squashes these last 8 bits, so they can be used to sample i - final int i = ((int) xx) & MASK_INT8; + final int i = (int) xx & MASK_INT8; if (i < I_MAX) { // Early exit. @@ -1174,7 +1174,7 @@ public abstract class ZigguratSampler implements SharedStateContinuousSampler { private int selectRegion() { final long x = nextLong(); // j in [0, 256) - final int j = ((int) x) & MASK_INT8; + final int j = (int) x & MASK_INT8; // map to j in [0, N] with N the number of layers of the ziggurat return x >= IPMF[j] ? MAP[j] & MASK_INT8 : j; } diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/StableSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/StableSamplerTest.java index a3510c0b..ef995554 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/StableSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/StableSamplerTest.java @@ -702,7 +702,7 @@ class StableSamplerTest { // Note: // The point at which there *should* be no difference between the two is when // exp(x) - 1 == exp(x). This will occur at exp(x)=2^54, x = ln(2^54) = 37.43. - Assertions.assertEquals(((double) (1L << 54)) - 1, (double) (1L << 54)); + Assertions.assertEquals((double) (1L << 54) - 1, (double) (1L << 54)); // However since expm1 and exp are only within 1 ULP of the exact result differences // still occur above this threshold. // 2^6 = 64; 2^-4 = 0.0625
