diff --git a/utils/src/main/java/li/strolch/utils/helper/MathHelper.java b/utils/src/main/java/li/strolch/utils/helper/MathHelper.java index e34f4b827..04653e9df 100644 --- a/utils/src/main/java/li/strolch/utils/helper/MathHelper.java +++ b/utils/src/main/java/li/strolch/utils/helper/MathHelper.java @@ -29,18 +29,20 @@ public class MathHelper { public static final double PRECISION = 1.0E08; public static final int PRECISION_DIGITS = 3; + public static boolean isEqualPrecision(double d1, double d2, double precision) { + return Double.compare(d1, d2) == 0 || Math.abs(d1 - d2) <= (1.0d / precision); + } + /** * Check if the two values are equal with respect to the precision * - * @param firstValue - * the first value to compare - * @param secondValue - * the second value to compare to + * @param firstValue the first value to compare + * @param secondValue the second value to compare to * * @return boolean True, if the two values are equal under the set precision. Fales, otherwise. */ public static boolean isEqualPrecision(double firstValue, double secondValue) { - return (java.lang.Math.abs(firstValue - secondValue) < (1.0d / PRECISION)); + return (java.lang.Math.abs(firstValue - secondValue) <= (1.0d / PRECISION)); } /** @@ -49,10 +51,8 @@ public class MathHelper { * the values are equal under the precision. Thus, it's efficient whenever the value is expected to be smaller than * the bound. * - * @param value - * The value to check - * @param bound - * The bound given + * @param value The value to check + * @param bound The bound given * * @return true, if value < bound under the given precision. False, otherwise. */ @@ -65,10 +65,8 @@ public class MathHelper { * the given bound.

Note: This implementation tests if value > bound and, if it is so, if equality does NOT * hold. Thus, it is efficient whenever the value is not expected to be greater than the bound. * - * @param value - * The value to check - * @param bound - * The bound given. + * @param value The value to check + * @param bound The bound given. * * @return true, if value > bound and the values do not test equal under precision. False, otherwise. */ @@ -80,14 +78,13 @@ public class MathHelper { *

Rounds the given double value to the number of decimals

* *

Warning: Do not use the returned value for further calculations. Always finish calculates and use one - * of the following methods:

to - * test on equality or greater than/ smaller than + * of the following methods:

to test on equality or greater than/ smaller than * - * @param value - * the double value to round - * @param decimals - * number of decimals + * @param value the double value to round + * @param decimals number of decimals * * @return the rounded number */ @@ -139,8 +136,7 @@ public class MathHelper { /** * Formatting a double to a string, without scientific notation * - * @param d - * the double to format + * @param d the double to format * * @return the formatted string */ diff --git a/utils/src/test/java/li/strolch/utils/MathHelperTest.java b/utils/src/test/java/li/strolch/utils/MathHelperTest.java new file mode 100644 index 000000000..c6add02a9 --- /dev/null +++ b/utils/src/test/java/li/strolch/utils/MathHelperTest.java @@ -0,0 +1,95 @@ +package li.strolch.utils; + +import org.junit.Test; + +import static li.strolch.utils.helper.MathHelper.*; +import static org.junit.Assert.*; + +public class MathHelperTest { + @Test + public void shouldRound1() { + assertEquals(1.0, toPrecision(1.00001), 0.0); + } + + @Test + public void shouldRound2() { + assertEquals(1.001, toPrecision(1.001), 0.0); + } + + @Test + public void shouldRound3() { + assertEquals(1.333, toPrecision(1.3333333), 0.0); + } + + @Test + public void shouldTestToPrecision1() { + assertEquals(1.0, toPrecision(1.00001, 3), 0.0); + } + + @Test + public void shouldTestToPrecision2() { + assertEquals(1.001, toPrecision(1.001, 3), 0.0); + } + + @Test + public void shouldTestToPrecision3() { + assertEquals(1.333, toPrecision(1.3333333, 3), 0.0); + } + + @Test + public void shouldTestEqualsPrecision1() { + assertTrue(isEqualPrecision(1.333333333, 1.33333333)); + } + + @Test + public void shouldTestEqualsPrecision2() { + assertTrue(isEqualPrecision(1.3333333, 1.333, 3)); + } + + @Test + public void shouldTestEqualsPrecision3() { + assertTrue(isEqualPrecision(1.033, 1.033435345345, 3)); + } + + @Test + public void shouldTestSmallerEqualPrecision1() { + assertTrue(isSmallerEqualPrecision(1.03333, 1.033435345345)); + } + + @Test + public void shouldTestSmallerEqualPrecision2() { + assertTrue(isSmallerEqualPrecision(1.033333338, 1.033333335)); + } + + @Test + public void shouldTestGreaterEqualPrecision1() { + assertTrue(isGreaterPrecision(1.03343537, 1.03343535)); + } + + @Test + public void shouldTestGreaterEqualPrecision2() { + assertTrue(isGreaterPrecision(1.03333335, 1.03333333)); + } + + @Test + public void shouldTestToPrecisionString1() { + assertEquals("1.033", toPrecisionString(1.03333335)); + } + + @Test + public void shouldTestToPrecisionString2() { + assertEquals("1.034", toPrecisionString(1.0344444)); + } + @Test + public void shouldTestGetNumberOfDecimalPlaces1() { + assertEquals(7, getNumberOfDecimalPlaces(1.0344444)); + } + @Test + public void shouldTestGetNumberOfDecimalPlaces2() { + assertEquals(3, getNumberOfDecimalPlaces(1.034)); + } + @Test + public void shouldTestDoubleToString() { + assertEquals("0.0000000453453335", doubleToString(0.0000000453453335)); + } +}