# HG changeset patch # User Jaroslav Tulach # Date 1359386812 -3600 # Node ID 4ff4e27465e0fe66586b6602cd601850bd833f7e # Parent d0f57d3ea898f0547d5ba748a92a34b6adf2c4d3 Math.rint emulated diff -r d0f57d3ea898 -r 4ff4e27465e0 emul/mini/src/main/java/java/lang/Math.java --- a/emul/mini/src/main/java/java/lang/Math.java Mon Jan 28 13:52:28 2013 +0100 +++ b/emul/mini/src/main/java/java/lang/Math.java Mon Jan 28 16:26:52 2013 +0100 @@ -375,6 +375,68 @@ public static double floor(double a) { throw new UnsupportedOperationException(); } + /** + * Computes the remainder operation on two arguments as prescribed + * by the IEEE 754 standard. + * The remainder value is mathematically equal to + * f1 - f2 × n, + * where n is the mathematical integer closest to the exact + * mathematical value of the quotient {@code f1/f2}, and if two + * mathematical integers are equally close to {@code f1/f2}, + * then n is the integer that is even. If the remainder is + * zero, its sign is the same as the sign of the first argument. + * Special cases: + * + * + * @param f1 the dividend. + * @param f2 the divisor. + * @return the remainder when {@code f1} is divided by + * {@code f2}. + */ +// public static double IEEEremainder(double f1, double f2) { +// return f1 % f2; +// } + + /** + * Returns the {@code double} value that is closest in value + * to the argument and is equal to a mathematical integer. If two + * {@code double} values that are mathematical integers are + * equally close, the result is the integer value that is + * even. Special cases: + * + * + * @param a a {@code double} value. + * @return the closest floating-point value to {@code a} that is + * equal to a mathematical integer. + */ + public static double rint(double a) { + double ceil = ceil(a); + double floor = floor(a); + + double dc = ceil - a; + double df = a - floor; + + if (dc < df) { + return ceil; + } else if (dc > df) { + return floor; + } + + int tenC = (int) (ceil % 10.0); + + if (tenC % 2 == 0) { + return ceil; + } else { + return floor; + } + } /** * Returns the angle theta from the conversion of rectangular diff -r d0f57d3ea898 -r 4ff4e27465e0 vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Mon Jan 28 13:52:28 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Mon Jan 28 16:26:52 2013 +0100 @@ -78,6 +78,73 @@ 3.0d, 1l ); } + + @Test public void rintNegativeUp() throws Exception { + final double cnts = -453904.634; + assertExec( + "Should round up to end with 5", + Math.class, "rint__DD", + -453905.0, cnts + ); + } + + @Test public void rintNegativeDown() throws Exception { + final double cnts = -453904.434; + assertExec( + "Should round up to end with 4", + Math.class, "rint__DD", + -453904.0, cnts + ); + } + + @Test public void rintPositiveUp() throws Exception { + final double cnts = 453904.634; + assertExec( + "Should round up to end with 5", + Math.class, "rint__DD", + 453905.0, cnts + ); + } + @Test public void rintPositiveDown() throws Exception { + final double cnts = 453904.434; + assertExec( + "Should round up to end with 4", + Math.class, "rint__DD", + 453904.0, cnts + ); + } + @Test public void rintOneHalf() throws Exception { + final double cnts = 1.5; + assertExec( + "Should round up to end with 2", + Math.class, "rint__DD", + 2.0, cnts + ); + } + @Test public void rintNegativeOneHalf() throws Exception { + final double cnts = -1.5; + assertExec( + "Should round up to end with 2", + Math.class, "rint__DD", + -2.0, cnts + ); + } + @Test public void rintTwoAndHalf() throws Exception { + final double cnts = 2.5; + assertExec( + "Should round up to end with 2", + Math.class, "rint__DD", + 2.0, cnts + ); + } + @Test public void rintNegativeTwoOneHalf() throws Exception { + final double cnts = -2.5; + assertExec( + "Should round up to end with 2", + Math.class, "rint__DD", + -2.0, cnts + ); + } @Test public void divAndRound() throws Exception { assertExec(