Skip to content

Commit 9b774ce

Browse files
committed
fix checkInvPow2
1 parent fdafc77 commit 9b774ce

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/main/java/org/apache/datasketches/common/Util.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,15 @@ public static long ceilingMultiple2expK(final long n, final int k) {
445445
}
446446

447447
/**
448-
* Computes the inverse integer power of 2: 1/(2^e) = 2^(-e).
449-
* @param e a positive value between 0 and 1023 inclusive
450-
* @return the inverse integer power of 2: 1/(2^e) = 2^(-e)
448+
* Computes the inverse integer power of 2: 1/(2^exp) = 2^(-exp).
449+
* @param exp a positive value between 0 and 1023 inclusive
450+
* @return the inverse integer power of 2: 1/(2^exp) = 2^(-exp)
451451
*/
452-
public static double invPow2(final int e) {
453-
assert (e | (1024 - e - 1)) >= 0 : "e cannot be negative or greater than 1023: " + e;
454-
return Double.longBitsToDouble((1023L - e) << 52);
452+
public static double invPow2(final int exp) {
453+
if ((exp | (1024 - exp - 1)) < 0) {
454+
throw new SketchesArgumentException("exp cannot be negative or greater than 1023: " + exp);
455+
}
456+
return Double.longBitsToDouble((1023L - exp) << 52);
455457
}
456458

457459
/**

src/test/java/org/apache/datasketches/common/UtilTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ public void checkIsMultipleOf8AndGT0() {
203203
public void checkInvPow2() {
204204
Assert.assertEquals(invPow2(1), 0.5);
205205
Assert.assertEquals(invPow2(0), 1.0);
206-
try { invPow2(-1); failIAE(); } catch (final AssertionError e) {}
207-
try {invPow2(1024); failIAE(); } catch (final AssertionError e) {}
208-
try {invPow2(Integer.MIN_VALUE); failIAE(); } catch (final AssertionError e) {}
206+
try {invPow2(-1); failIAE(); } catch (final SketchesArgumentException e) {}
207+
try {invPow2(1024); failIAE(); } catch (final SketchesArgumentException e) {}
208+
try {invPow2(Integer.MIN_VALUE); failIAE(); } catch (final SketchesArgumentException e) {}
209209
}
210210

211211
private static void failIAE() { throw new IllegalArgumentException("Test should have failed!"); }

0 commit comments

Comments
 (0)