Skip to content

Commit

Permalink
Merge pull request #5374 from cd2357/mathutils-test
Browse files Browse the repository at this point in the history
Handle rounding of infinite Doubles
  • Loading branch information
ripcurlx authored Apr 13, 2021
2 parents ca2731e + 5e73774 commit e516992
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/src/main/java/bisq/common/util/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static double roundDouble(double value, int precision) {
public static double roundDouble(double value, int precision, RoundingMode roundingMode) {
if (precision < 0)
throw new IllegalArgumentException();
if (!Double.isFinite(value))
throw new IllegalArgumentException("Expected a finite double, but found " + value);

try {
BigDecimal bd = BigDecimal.valueOf(value);
Expand Down
14 changes: 14 additions & 0 deletions common/src/test/java/bisq/common/util/MathUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@

public class MathUtilsTest {

@Test(expected = IllegalArgumentException.class)
public void testRoundDoubleWithInfiniteArg() {
MathUtils.roundDouble(Double.POSITIVE_INFINITY, 2);
}

@Test(expected = IllegalArgumentException.class)
public void testRoundDoubleWithNaNArg() {
MathUtils.roundDouble(Double.NaN, 2);
}

@Test(expected = IllegalArgumentException.class)
public void testRoundDoubleWithNegativePrecision() {
MathUtils.roundDouble(3, -1);
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
Expand Down

0 comments on commit e516992

Please sign in to comment.