1.1 --- a/emul/mini/src/main/java/java/lang/Math.java Mon Jan 28 13:52:28 2013 +0100
1.2 +++ b/emul/mini/src/main/java/java/lang/Math.java Mon Jan 28 16:26:52 2013 +0100
1.3 @@ -375,6 +375,68 @@
1.4 public static double floor(double a) {
1.5 throw new UnsupportedOperationException();
1.6 }
1.7 + /**
1.8 + * Computes the remainder operation on two arguments as prescribed
1.9 + * by the IEEE 754 standard.
1.10 + * The remainder value is mathematically equal to
1.11 + * <code>f1 - f2</code> × <i>n</i>,
1.12 + * where <i>n</i> is the mathematical integer closest to the exact
1.13 + * mathematical value of the quotient {@code f1/f2}, and if two
1.14 + * mathematical integers are equally close to {@code f1/f2},
1.15 + * then <i>n</i> is the integer that is even. If the remainder is
1.16 + * zero, its sign is the same as the sign of the first argument.
1.17 + * Special cases:
1.18 + * <ul><li>If either argument is NaN, or the first argument is infinite,
1.19 + * or the second argument is positive zero or negative zero, then the
1.20 + * result is NaN.
1.21 + * <li>If the first argument is finite and the second argument is
1.22 + * infinite, then the result is the same as the first argument.</ul>
1.23 + *
1.24 + * @param f1 the dividend.
1.25 + * @param f2 the divisor.
1.26 + * @return the remainder when {@code f1} is divided by
1.27 + * {@code f2}.
1.28 + */
1.29 +// public static double IEEEremainder(double f1, double f2) {
1.30 +// return f1 % f2;
1.31 +// }
1.32 +
1.33 + /**
1.34 + * Returns the {@code double} value that is closest in value
1.35 + * to the argument and is equal to a mathematical integer. If two
1.36 + * {@code double} values that are mathematical integers are
1.37 + * equally close, the result is the integer value that is
1.38 + * even. Special cases:
1.39 + * <ul><li>If the argument value is already equal to a mathematical
1.40 + * integer, then the result is the same as the argument.
1.41 + * <li>If the argument is NaN or an infinity or positive zero or negative
1.42 + * zero, then the result is the same as the argument.</ul>
1.43 + *
1.44 + * @param a a {@code double} value.
1.45 + * @return the closest floating-point value to {@code a} that is
1.46 + * equal to a mathematical integer.
1.47 + */
1.48 + public static double rint(double a) {
1.49 + double ceil = ceil(a);
1.50 + double floor = floor(a);
1.51 +
1.52 + double dc = ceil - a;
1.53 + double df = a - floor;
1.54 +
1.55 + if (dc < df) {
1.56 + return ceil;
1.57 + } else if (dc > df) {
1.58 + return floor;
1.59 + }
1.60 +
1.61 + int tenC = (int) (ceil % 10.0);
1.62 +
1.63 + if (tenC % 2 == 0) {
1.64 + return ceil;
1.65 + } else {
1.66 + return floor;
1.67 + }
1.68 + }
1.69
1.70 /**
1.71 * Returns the angle <i>theta</i> from the conversion of rectangular
2.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Mon Jan 28 13:52:28 2013 +0100
2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Mon Jan 28 16:26:52 2013 +0100
2.3 @@ -78,6 +78,73 @@
2.4 3.0d, 1l
2.5 );
2.6 }
2.7 +
2.8 + @Test public void rintNegativeUp() throws Exception {
2.9 + final double cnts = -453904.634;
2.10 + assertExec(
2.11 + "Should round up to end with 5",
2.12 + Math.class, "rint__DD",
2.13 + -453905.0, cnts
2.14 + );
2.15 + }
2.16 +
2.17 + @Test public void rintNegativeDown() throws Exception {
2.18 + final double cnts = -453904.434;
2.19 + assertExec(
2.20 + "Should round up to end with 4",
2.21 + Math.class, "rint__DD",
2.22 + -453904.0, cnts
2.23 + );
2.24 + }
2.25 +
2.26 + @Test public void rintPositiveUp() throws Exception {
2.27 + final double cnts = 453904.634;
2.28 + assertExec(
2.29 + "Should round up to end with 5",
2.30 + Math.class, "rint__DD",
2.31 + 453905.0, cnts
2.32 + );
2.33 + }
2.34 + @Test public void rintPositiveDown() throws Exception {
2.35 + final double cnts = 453904.434;
2.36 + assertExec(
2.37 + "Should round up to end with 4",
2.38 + Math.class, "rint__DD",
2.39 + 453904.0, cnts
2.40 + );
2.41 + }
2.42 + @Test public void rintOneHalf() throws Exception {
2.43 + final double cnts = 1.5;
2.44 + assertExec(
2.45 + "Should round up to end with 2",
2.46 + Math.class, "rint__DD",
2.47 + 2.0, cnts
2.48 + );
2.49 + }
2.50 + @Test public void rintNegativeOneHalf() throws Exception {
2.51 + final double cnts = -1.5;
2.52 + assertExec(
2.53 + "Should round up to end with 2",
2.54 + Math.class, "rint__DD",
2.55 + -2.0, cnts
2.56 + );
2.57 + }
2.58 + @Test public void rintTwoAndHalf() throws Exception {
2.59 + final double cnts = 2.5;
2.60 + assertExec(
2.61 + "Should round up to end with 2",
2.62 + Math.class, "rint__DD",
2.63 + 2.0, cnts
2.64 + );
2.65 + }
2.66 + @Test public void rintNegativeTwoOneHalf() throws Exception {
2.67 + final double cnts = -2.5;
2.68 + assertExec(
2.69 + "Should round up to end with 2",
2.70 + Math.class, "rint__DD",
2.71 + -2.0, cnts
2.72 + );
2.73 + }
2.74
2.75 @Test public void divAndRound() throws Exception {
2.76 assertExec(