Return-Path: Delivered-To: apmail-incubator-harmony-commits-archive@www.apache.org Received: (qmail 77747 invoked from network); 7 Apr 2006 09:36:32 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 7 Apr 2006 09:36:32 -0000 Received: (qmail 77274 invoked by uid 500); 7 Apr 2006 09:36:31 -0000 Delivered-To: apmail-incubator-harmony-commits-archive@incubator.apache.org Received: (qmail 77222 invoked by uid 500); 7 Apr 2006 09:36:31 -0000 Mailing-List: contact harmony-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: harmony-dev@incubator.apache.org Delivered-To: mailing list harmony-commits@incubator.apache.org Received: (qmail 77211 invoked by uid 99); 7 Apr 2006 09:36:31 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 07 Apr 2006 02:36:31 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Fri, 07 Apr 2006 02:36:30 -0700 Received: (qmail 77542 invoked by uid 65534); 7 Apr 2006 09:36:09 -0000 Message-ID: <20060407093609.77540.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Fri, 07 Apr 2006 09:36:09 -0000 To: harmony-commits@incubator.apache.org From: tellison@apache.org X-Mailer: svnmailer-1.0.7 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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. + *

+ * Double is the wrapper for the primitive type double. + *

+ * + * @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. - */ + /** + *

+ * Constant for the maximum double value, (2 - 2-52/sup>) * 21023. + *

+ */ public static final double MAX_VALUE = 1.79769313486231570e+308; + /** + *

+ * Constant for the minimum double value, 2-1074. + *

+ */ 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) - */ + /** + *

+ * Constant for the Not-a-Number (NaN) value of the double type. + *

+ */ public static final double NaN = 0.0 / 0.0; - /** - * Values to represent infinite results - */ + /** + *

+ * Constant for the Positive Infinity value of the double type. + *

+ */ public static final double POSITIVE_INFINITY = 1.0 / 0.0; + /** + *

+ * Constant for the Negative Infinity value of the double type. + *

+ */ 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"; + /** + *

+ * Constant for the number of bits to represent a double in + * two's compliment form. + *

+ * + * @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; } + + /** + *

+ * Returns a Double instance for the double + * value passed. This method is preferred over the constructor, as this + * method may maintain a cache of instances. + *

+ * + * @param d The double value. + * @return A Double instance. + * @since 1.5 + */ + public static Double valueOf(double d) { + return new Double(d); + } + + /** + *

+ * Converts a double into a hexadecimal string representation. + *

+ * + * @param d The double to convert. + * @return The hexadecimal string representation of f. + * @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++; + } + } }