1
0
Fork 0
mirror of https://github.com/bisq-network/bisq.git synced 2025-03-13 11:09:10 +01:00

Handle rounding of infinite Doubles

In certain situations we deal with infinite (or NaN) doubles. This causes issues when trying to instantiate a BigDecimal from it, which is an intermediary step in the process of rounding the double.

This commit checks for such non-finite doubles and throws a specific exception if their rounding is attempted.
This commit is contained in:
cd2357 2021-03-29 19:44:09 +02:00
parent 58d93f6e04
commit 5e7377479e
No known key found for this signature in database
GPG key ID: F26C56748514D0D3
2 changed files with 16 additions and 0 deletions
common/src
main/java/bisq/common/util
test/java/bisq/common/util

View file

@ -42,6 +42,8 @@ public class MathUtils {
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);

View file

@ -24,6 +24,20 @@ import static org.junit.Assert.assertFalse;
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