Skip to content

Commit

Permalink
Fixed a bug in Scaler
Browse files Browse the repository at this point in the history
  • Loading branch information
leventov committed Nov 1, 2014
1 parent 736bd13 commit 639b405
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private static void check(long n) {
}

private static final BigDecimal LONG_MAX_VALUE = valueOf(Long.MAX_VALUE);
private static final BigDecimal INT_MAX_VALUE = valueOf(Integer.MAX_VALUE);

final double scale;
private BigDecimal scaleAsBD;
Expand All @@ -158,13 +159,19 @@ BigDecimal createBD() {

public int scaleUpper(int n) {
check(n);
int lower = (int) ((double) n * scale);
int lower = scaleLower(n);
return lower < Integer.MAX_VALUE ? lower + 1 : Integer.MAX_VALUE;
}

public int scaleLower(int n) {
check(n);
return (int) ((double) n * scale);
double d = ((double) n) * scale;
if (d != Math.rint(d)) {
return (int) d;
} else {
BigDecimal lower = valueOf(n).multiply(scaleAsBD());
return lower.compareTo(INT_MAX_VALUE) < 0 ? lower.intValue() : Integer.MAX_VALUE;
}
}

public long scaleUpper(long n) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public class ScalerTest {
public static final Collection<Integer> ints = new ArrayList<Integer>();
public static final Collection<Long> longs = new ArrayList<Long>();
static {
// special property of this value: multiplied by Math.PI, it results to a double value
// which is actually an integer, i. e. d == Math.rint(d)
ints.add(415069444);

for (int i = 1; i < 10000; i++) {
ints.add(i);
longs.add((long) i);
Expand Down

0 comments on commit 639b405

Please sign in to comment.