revert back to 2420 for Double[Test].java, until I can unwind the lossage...
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@2434 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/lang/Double.java b/user/super/com/google/gwt/emul/java/lang/Double.java
index 23957fc..ce65260 100644
--- a/user/super/com/google/gwt/emul/java/lang/Double.java
+++ b/user/super/com/google/gwt/emul/java/lang/Double.java
@@ -19,37 +19,18 @@
* Wraps a primitive <code>double</code> as an object.
*/
public final class Double extends Number implements Comparable<Double> {
- public static final int MAX_EXPONENT = 1023; // a JDK 1.6 constant
- // ==Math.getExponent(Double.MAX_VALUE);
public static final double MAX_VALUE = 1.7976931348623157e+308;
- public static final int MIN_EXPONENT = -1022; // a JDK 1.6 constant
- // ==Math.getExponent(Double.MIN_NORMAL);;
- public static final double MIN_NORMAL = 2.2250738585072014e-308;
- // a JDK 1.6 constant
public static final double MIN_VALUE = 4.9e-324;
+ public static final double MIN_NORMAL = 2.2250738585072014e-308;
+ public static final int MAX_EXPONENT = 1023;
+ // ==Math.getExponent(Double.MAX_VALUE);
+ public static final int MIN_EXPONENT = -1022;
+ // ==Math.getExponent(Double.MIN_NORMAL);;
public static final double NaN = 0d / 0d;
public static final double NEGATIVE_INFINITY = -1d / 0d;
public static final double POSITIVE_INFINITY = 1d / 0d;
public static final int SIZE = 64;
- static final int EXPONENT_BITSIZE = 11;
- // the extra -1 is for the sign bit
- static final int MANTISSA_BITSIZE = SIZE - EXPONENT_BITSIZE - 1;
- // the exponent is biased by one less than its midpoint, e.g. 2^11 / 2 - 1;
- static final int EXPONENT_BIAS = 1 << (EXPONENT_BITSIZE - 1) - 1;
- // the mask is all 1 bits in the exponent, e.g. 0x7ff shifted over by 52
- static final long EXPONENT_MASK = ((1L
- << EXPONENT_BITSIZE) - 1) << MANTISSA_BITSIZE;
- // place 1-bit in top position
- static final long NAN_MANTISSA = 1L << (MANTISSA_BITSIZE - 1);
- // sign bit is the MSB bit
- static final long SIGN_BIT = 0x1L << (SIZE - 1);
- // Zero represented in biased form
- static final int BIASED_ZERO_EXPONENT = EXPONENT_BIAS;
- // The maximum mantissa value, represented as a double
- static final double MAX_MANTISSA_VALUE = Math.pow(2, MANTISSA_BITSIZE);
- // The mantissa of size MANTISSA_BITSIZE with all bits set to 1_
- static final long MANTISSA_MASK = (1L << MANTISSA_BITSIZE) - 1;
public static int compare(double x, double y) {
if (x < y) {
@@ -61,83 +42,6 @@
}
}
- // Theory of operation: Let a double number d be represented as
- // 1.M * 2^E, where the leading bit is assumed to be 1,
- // the fractional mantissa M is multiplied 2 to the power of E.
- // We want to reliably recover M and E, and then encode them according
- // to IEEE754 (see http://en.wikipedia.org/wiki/IEEE754)
- public static long doubleToLongBits(final double d) {
-
- long sign = (d < 0 ? SIGN_BIT : 0);
- long exponent = 0;
- double absV = Math.abs(d);
-
- if (Double.isNaN(d)) {
- // IEEE754, NaN exponent bits all 1s, and mantissa is non-zero
- return EXPONENT_MASK | NAN_MANTISSA;
- }
- if (Double.isInfinite(d)) {
- // an infinite number is a number with a zero mantissa and all
- // exponent bits set to 1
- exponent = EXPONENT_MASK;
- absV = 0.0;
- } else {
- if (absV == 0.0) {
- // IEEE754, exponent is 0, mantissa is zero
- // we don't handle negative zero at the moment, it is treated as
- // positive zero
- exponent = 0L;
- } else {
- // get an approximation to the exponent
- // if d = 1.M * 2^E then
- // log2(d) = log(1.M) + log2(2^E) = log(1.M) + E
- // floor(log(1.M) + E) = E because log(1.M) always < 1
- // it may turn out log2(x) = log(x)/log(2) always returns the
- // the correct exponent, but this method is more defensive
- // with respect to precision to avoid off by 1 problems
- int guess = (int) Math.floor(Math.log(absV) / Math.log(2));
- // force it to MAX_EXPONENT, MAX_EXPONENT interval
- // (<= -MAX_EXPONENT = denorm/zero)
- guess = Math.max(-MAX_EXPONENT, Math.min(guess, MAX_EXPONENT));
-
- // Recall that d = 1.M * 2^E, so dividing by 2^E should leave
- // us with 1.M
- double exp = Math.pow(2, guess);
- absV = absV / exp;
-
- // while the number is still bigger than a normalized number
- // increment exponent guess
- // This might occur if there is some precision loss in determining
- // the exponent
- while (absV > 2.0) {
- guess++;
- absV /= 2.0;
- }
- // if the number is smaller than a normalized number
- // decrement exponent. If the exponent becomes zero, and we
- // fail to achieve a normalized mantissa, then this number
- // must be a denormalized value
- while (absV < 1 && guess > 0) {
- guess--;
- absV *= 2;
- }
- exponent = (guess + EXPONENT_BIAS) << MANTISSA_BITSIZE;
- }
- }
- // if denormalized
- if (exponent <= BIASED_ZERO_EXPONENT) {
- // denormalized numbers have an exponent of zero, but pretend
- // they have an exponent of 1, so since there is an implicit
- // * 2^1 for denorms, we correct by dividing by 2
- absV /= 2;
- }
- // the input value has now been stripped of its exponent
- // and is in the range [1,2), we strip off the leading decimal to normalize
- // and use the remainer as a percentage of the significand value (2^52)
- long mantissa = (long) ((absV % 1) * MAX_MANTISSA_VALUE);
- return sign | exponent | (mantissa & MANTISSA_MASK);
- }
-
/**
* @skip Here for shared implementation with Arrays.hashCode
*/
@@ -153,32 +57,6 @@
return isNaN(x);
}-*/;
- public static double longBitsToDouble(long value) {
- // exponent in MSB bits 1-11
- int exp = (int) ((value & EXPONENT_MASK) >> MANTISSA_BITSIZE);
- // unbias exponent handle denorm case
- int denorm = (exp == 0 ? 1 : 0);
- // denorm exponent becomes -1022
- exp = exp - EXPONENT_BIAS + denorm;
- // mantissa in LSB 52 bits
- long mantissa = (value & MANTISSA_MASK);
- // sign in MSB bit 0
- int sign = (value & SIGN_BIT) != 0 ? -1 : 1;
- // unbiased exponent value of EXPONENT_BIAS + 1 (e.g. 1024)
- // is equivalent to all 1 bits in biased exp (e.g. 2047)
- if (exp == EXPONENT_BIAS + 1) {
- if (mantissa != 0) {
- return Double.NaN;
- } else {
- return sign < 0 ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
- }
- }
- // non-denormized numbers get 1.0 added back, since our first digit is
- // always a 1
- // mantissa is divided by 2^52, and multiplied by 2^exponent
- return sign * ((mantissa / MAX_MANTISSA_VALUE + (1 - denorm)) * Math.pow(2, exp));
- }
-
public static double parseDouble(String s) throws NumberFormatException {
return __parseAndValidateDouble(s);
}
diff --git a/user/test/com/google/gwt/emultest/java/lang/DoubleTest.java b/user/test/com/google/gwt/emultest/java/lang/DoubleTest.java
index 887fde6..283763b 100644
--- a/user/test/com/google/gwt/emultest/java/lang/DoubleTest.java
+++ b/user/test/com/google/gwt/emultest/java/lang/DoubleTest.java
@@ -23,21 +23,6 @@
*/
public class DoubleTest extends GWTTestCase {
- // Some actual results from JDK1.6 VM doubleToLongBits calls
- private static final long NAN_LONG_VALUE = 0x7ff8000000000000L;
- private static final long POSINF_LONG_VALUE = 0x7ff0000000000000L;
- private static final long NEGINF_LONG_VALUE = 0xfff0000000000000L;
- private static final long MAXD_LONG_VALUE = 0x7fefffffffffffffL;
- private static final long MIND_LONG_VALUE = 0x1L;
- private static final long MINNORM_LONG_VALUE = 0x10000000000000L;
- private static final double TEST1_DOUBLE_VALUE = 2.3e27;
- private static final long TEST1_LONG_VALUE = 0x459dba0fc757e49cL;
- private static final long NEGTEST1_LONG_VALUE = 0xc59dba0fc757e49cL;
-
- // TODO(fabbott): this constants are from the JDK 1.6 Double, so we can't rely on them
- // when we build on 1.5! But when we *do* support 1.6, this def'n should go away
- public static final double MIN_NORMAL = 2.2250738585072014e-308;
-
public String getModuleName() {
return "com.google.gwt.emultest.EmulSuite";
}
@@ -73,61 +58,20 @@
assertTrue(Double.MIN_VALUE < Double.MAX_VALUE);
assertFalse(Double.NaN == Double.NaN);
assertEquals(64, Double.SIZE);
- // jdk1.6 assertEquals(Math.getExponent(Double.MAX_VALUE), Double.MAX_EXPONENT);
- // jdk1.6 assertEquals(Math.getExponent(Double.MIN_NORMAL), Double.MIN_EXPONENT);
- }
-
- public void testDoubleToLongBits() {
- assertEquals("NaN double->longbits test",
- NAN_LONG_VALUE, Double.doubleToLongBits(Double.NaN));
- assertEquals("posinf double->longbits test",
- POSINF_LONG_VALUE, Double.doubleToLongBits(Double.POSITIVE_INFINITY));
- assertEquals("neginf double->longbits test",
- NEGINF_LONG_VALUE, Double.doubleToLongBits(Double.NEGATIVE_INFINITY));
- assertEquals("maxvalue double->longbits test",
- MAXD_LONG_VALUE, Double.doubleToLongBits(Double.MAX_VALUE));
- assertEquals("minvalue double->longbits test",
- MIND_LONG_VALUE, Double.doubleToLongBits(Double.MIN_VALUE));
- assertEquals("test1 double->longbits test",
- TEST1_LONG_VALUE, Double.doubleToLongBits(TEST1_DOUBLE_VALUE));
- assertEquals("-test1 double->longbits test",
- NEGTEST1_LONG_VALUE, Double.doubleToLongBits(-TEST1_DOUBLE_VALUE));
- // TODO(fabbott): swap back to Double.MIN_NORMAL when we use jdk 1.6
- assertEquals("minnormal double->longbits test",
- MINNORM_LONG_VALUE, Double.doubleToLongBits(MIN_NORMAL));
- }
-
- public void testLongBitsToDouble() {
- assertTrue("isNaN longbits->double test",
- Double.isNaN(Double.longBitsToDouble(NAN_LONG_VALUE)));
- assertEquals("posinf longbits->double test",
- Double.POSITIVE_INFINITY, Double.longBitsToDouble(POSINF_LONG_VALUE));
- assertEquals("neginf longbits->double test",
- Double.NEGATIVE_INFINITY, Double.longBitsToDouble(NEGINF_LONG_VALUE));
- assertEquals("maxval longbits->double test",
- Double.MAX_VALUE, Double.longBitsToDouble(MAXD_LONG_VALUE));
- assertEquals("minval longbits->double test",
- Double.MIN_VALUE, Double.longBitsToDouble(MIND_LONG_VALUE));
- assertEquals("test1 longbits->double test",
- TEST1_DOUBLE_VALUE, Double.longBitsToDouble(TEST1_LONG_VALUE));
- assertEquals("-test1 longbits->double test",
- -TEST1_DOUBLE_VALUE, Double.longBitsToDouble(NEGTEST1_LONG_VALUE));
- // TODO(fabbott): swap back to Double.MIN_NORMAL when we use jdk 1.6
- assertEquals("minnormal longbits->double test",
- MIN_NORMAL, Double.longBitsToDouble(MINNORM_LONG_VALUE));
+ // jdk1.6 assertEquals(Math.getExponent(Double.MAX_VALUE),
+ // Double.MAX_EXPONENT);
+ // jdk1.6 assertEquals(Math.getExponent(Double.MIN_NORMAL),
+ // Double.MIN_EXPONENT);
}
public void testParse() {
- assertEquals(0.0, Double.parseDouble("0"));
- assertEquals(-1.5, Double.parseDouble("-1.5"));
- assertEquals(3.0, Double.parseDouble("3."));
- assertEquals(0.5, Double.parseDouble(".5"));
- assertEquals("parse of 2.98e8",
- 2.98e8, Double.parseDouble("2.98e8"));
- assertEquals("parse of -2.98e-8",
- -2.98e-8, Double.parseDouble("-2.98e-8"));
- assertEquals("parse of 2.08E+8",
- +2.98E+8, Double.parseDouble("+2.98E+8"));
+ assertTrue(0 == Double.parseDouble("0"));
+ assertTrue(-1.5 == Double.parseDouble("-1.5"));
+ assertTrue(3.0 == Double.parseDouble("3."));
+ assertTrue(0.5 == Double.parseDouble(".5"));
+ assertTrue(2.98e8 == Double.parseDouble("2.98e8"));
+ assertTrue(-2.98e-8 == Double.parseDouble("-2.98e-8"));
+ assertTrue(+2.98E+8 == Double.parseDouble("+2.98E+8"));
assertTrue(
"Can't parse MIN_VALUE",
Double.MIN_VALUE == Double.parseDouble(String.valueOf(Double.MIN_VALUE)));