You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/04/07 11:36:09 UTC
svn commit: r392229 - in
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/lang/Double.java
test/java/org/apache/harmony/tests/java/lang/DoubleTest.java
Author: tellison
Date: Fri Apr 7 02:36:05 2006
New Revision: 392229
URL: http://svn.apache.org/viewcvs?rev=392229&view=rev
Log:
Apply patch HARMONY-244 ([classlib][luni] Java 5 enhancements for java.lang.Double)
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Double.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/DoubleTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Double.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Double.java?rev=392229&r1=392228&r2=392229&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Double.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Double.java Fri Apr 7 02:36:05 2006
@@ -15,9 +15,13 @@
package java.lang;
-
/**
- * Doubles are objects (non-base types) which represent double values.
+ * <p>
+ * Double is the wrapper for the primitive type <code>double</code>.
+ * </p>
+ *
+ * @see java.lang.Number
+ * @since 1.0
*/
public final class Double extends Number implements Comparable {
@@ -26,32 +30,48 @@
/**
* The value which the receiver represents.
*/
- final double value;
+ private final double value;
- /**
- * Largest and smallest possible double values.
- */
+ /**
+ * <p>
+ * Constant for the maximum <code>double</code> value, (2 - 2<sup>-52/sup>) * 2<sup>1023</sup>.
+ * </p>
+ */
public static final double MAX_VALUE = 1.79769313486231570e+308;
+ /**
+ * <p>
+ * Constant for the minimum <code>double</code> value, 2<sup>-1074</sup>.
+ * </p>
+ */
public static final double MIN_VALUE = 5e-324;
/* 4.94065645841246544e-324 gets rounded to 9.88131e-324 */
- /**
- * A value which represents all invalid double results (NaN ==> Not a
- * Number)
- */
+ /**
+ * <p>
+ * Constant for the Not-a-Number (NaN) value of the <code>double</code> type.
+ * </p>
+ */
public static final double NaN = 0.0 / 0.0;
- /**
- * Values to represent infinite results
- */
+ /**
+ * <p>
+ * Constant for the Positive Infinity value of the <code>double</code> type.
+ * </p>
+ */
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
+ /**
+ * <p>
+ * Constant for the Negative Infinity value of the <code>double</code> type.
+ * </p>
+ */
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
/**
* The java.lang.Class that represents this class.
+ * @since 1.1
*/
public static final Class TYPE = new double[0].getClass()
.getComponentType();
@@ -59,6 +79,16 @@
// Note: This can't be set to "double.class", since *that* is
// defined to be "java.lang.Double.TYPE";
+ /**
+ * <p>
+ * Constant for the number of bits to represent a <code>double</code> in
+ * two's compliment form.
+ * </p>
+ *
+ * @since 1.5
+ */
+ public static final int SIZE = 64;
+
/**
* Constructs a new instance of the receiver which represents the double
* valued argument.
@@ -380,4 +410,95 @@
}
return double1 > double2 ? 1 : -1;
}
+
+ /**
+ * <p>
+ * Returns a <code>Double</code> instance for the <code>double</code>
+ * value passed. This method is preferred over the constructor, as this
+ * method may maintain a cache of instances.
+ * </p>
+ *
+ * @param d The double value.
+ * @return A <code>Double</code> instance.
+ * @since 1.5
+ */
+ public static Double valueOf(double d) {
+ return new Double(d);
+ }
+
+ /**
+ * <p>
+ * Converts a <code>double</code> into a hexadecimal string representation.
+ * </p>
+ *
+ * @param d The <code>double</code> to convert.
+ * @return The hexadecimal string representation of <code>f</code>.
+ * @since 1.5
+ */
+ public static String toHexString(double d) {
+ /*
+ * Reference: http://en.wikipedia.org/wiki/IEEE_754
+ */
+ if (d != d) // isNaN
+ return "NaN";
+ if (d == POSITIVE_INFINITY)
+ return "Infinity";
+ if (d == NEGATIVE_INFINITY)
+ return "-Infinity";
+
+ long bitValue = doubleToLongBits(d);
+
+ boolean negative = (bitValue & 0x8000000000000000L) != 0;
+ // mask exponent bits and shift down
+ long exponent = (bitValue & 0x7FF0000000000000L) >>> 52;
+ // mask significand bits and shift up
+ long significand = bitValue & 0x000FFFFFFFFFFFFFL;
+
+ if (exponent == 0 && significand == 0)
+ return (negative ? "-0x0.0p0" : "0x0.0p0");
+
+ StringBuilder hexString = new StringBuilder(10);
+ if (negative)
+ hexString.append("-0x");
+ else
+ hexString.append("0x");
+
+ if (exponent == 0) { // denormal (subnormal) value
+ hexString.append("0.");
+ // significand is 52-bits, so there can be 13 hex digits
+ int fractionDigits = 13;
+ // remove trailing hex zeros, so Integer.toHexString() won't print
+ // them
+ while ((significand != 0) && ((significand & 0xF) == 0)) {
+ significand >>>= 4;
+ fractionDigits--;
+ }
+ // this assumes Integer.toHexString() returns lowercase characters
+ String hexSignificand = Long.toHexString(significand);
+
+ // if there are digits left, then insert some '0' chars first
+ if (fractionDigits > hexSignificand.length()) {
+ int digitDiff = fractionDigits - hexSignificand.length();
+ while (digitDiff-- != 0)
+ hexString.append('0');
+ }
+ hexString.append(hexSignificand);
+ hexString.append("p-1022");
+ } else { // normal value
+ hexString.append("1.");
+ // remove trailing hex zeros, so Integer.toHexString() won't print
+ // them
+ while ((significand != 0) && ((significand & 0xF) == 0)) {
+ significand >>>= 4;
+ }
+ // this assumes Integer.toHexString() returns lowercase characters
+ String hexSignificand = Long.toHexString(significand);
+
+ hexString.append(hexSignificand);
+ hexString.append('p');
+ // remove exponent's 'bias' and convert to a string
+ hexString.append(Long.toString(exponent - 1023));
+ }
+ return hexString.toString();
+ }
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/DoubleTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/DoubleTest.java?rev=392229&r1=392228&r2=392229&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/DoubleTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/DoubleTest.java Fri Apr 7 02:36:05 2006
@@ -75,4 +75,49 @@
assertTrue("Assert 5: -0d should not be equals() 0d",
!new Double(0d).equals(new Double(-0d)));
}
+
+ /**
+ * @tests java.lang.Double#toHexString(double)
+ */
+ public void test_toHexStringF() {
+ //the follow values come from the Double Javadoc/Spec
+ assertEquals("0x0.0p0", Double.toHexString(0.0D));
+ assertEquals("-0x0.0p0", Double.toHexString(-0.0D));
+ assertEquals("0x1.0p0", Double.toHexString(1.0D));
+ assertEquals("-0x1.0p0", Double.toHexString(-1.0D));
+ assertEquals("0x1.0p1", Double.toHexString(2.0D));
+ assertEquals("0x1.8p1", Double.toHexString(3.0D));
+ assertEquals("0x1.0p-1", Double.toHexString(0.5D));
+ assertEquals("0x1.0p-2", Double.toHexString(0.25D));
+ assertEquals("0x1.fffffffffffffp1023", Double.toHexString(Double.MAX_VALUE));
+ assertEquals("0x0.0000000000001p-1022", Double.toHexString(Double.MIN_VALUE));
+
+ //test edge cases
+ assertEquals("NaN", Double.toHexString(Double.NaN));
+ assertEquals("-Infinity", Double.toHexString(Double.NEGATIVE_INFINITY));
+ assertEquals("Infinity", Double.toHexString(Double.POSITIVE_INFINITY));
+
+ //test various numbers
+ assertEquals("-0x1.da8p6", Double.toHexString(-118.625D));
+ assertEquals("0x1.2957874cccccdp23", Double.toHexString(9743299.65D));
+ assertEquals("0x1.2957874cccccdp23", Double.toHexString(9743299.65000D));
+ assertEquals("0x1.2957874cccf63p23", Double.toHexString(9743299.650001234D));
+ assertEquals("0x1.700d1061d3333p33", Double.toHexString(12349743299.65000D));
+ }
+
+ /**
+ * @tests java.lang.Double#valueOf(double)
+ */
+ public void test_valueOfD() {
+ assertEquals(new Double(Double.MIN_VALUE), Double.valueOf(Double.MIN_VALUE));
+ assertEquals(new Double(Double.MAX_VALUE), Double.valueOf(Double.MAX_VALUE));
+ assertEquals(new Double(0), Double.valueOf(0));
+
+ int s = -128;
+ while (s < 128) {
+ assertEquals(new Double(s), Double.valueOf(s));
+ assertEquals(new Double(s + 0.1D), Double.valueOf(s + 0.1D));
+ s++;
+ }
+ }
}