src/share/classes/java/lang/Double.java

Print this page




 259      * <tr><td>{@code Double.MAX_VALUE}</td>
 260      *     <td>{@code 0x1.fffffffffffffp1023}</td>
 261      * <tr><td>{@code Minimum Normal Value}</td>
 262      *     <td>{@code 0x1.0p-1022}</td>
 263      * <tr><td>{@code Maximum Subnormal Value}</td>
 264      *     <td>{@code 0x0.fffffffffffffp-1022}</td>
 265      * <tr><td>{@code Double.MIN_VALUE}</td>
 266      *     <td>{@code 0x0.0000000000001p-1022}</td>
 267      * </table>
 268      * @param   d   the {@code double} to be converted.
 269      * @return a hex string representation of the argument.
 270      * @since 1.5
 271      * @author Joseph D. Darcy
 272      */
 273     public static String toHexString(double d) {
 274         /*
 275          * Modeled after the "a" conversion specifier in C99, section
 276          * 7.19.6.1; however, the output of this method is more
 277          * tightly specified.
 278          */
 279         if (!FpUtils.isFinite(d) )
 280             // For infinity and NaN, use the decimal output.
 281             return Double.toString(d);
 282         else {
 283             // Initialized to maximum size of output.
 284             StringBuffer answer = new StringBuffer(24);
 285 
 286             if (Math.copySign(1.0, d) == -1.0)    // value is negative,
 287                 answer.append("-");                  // so append sign info
 288 
 289             answer.append("0x");
 290 
 291             d = Math.abs(d);
 292 
 293             if(d == 0.0) {
 294                 answer.append("0.0p0");
 295             }
 296             else {
 297                 boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
 298 
 299                 // Isolate significand bits and OR in a high-order bit


 531      * @return the {@code double} value represented by the string
 532      *         argument.
 533      * @throws NullPointerException  if the string is null
 534      * @throws NumberFormatException if the string does not contain
 535      *         a parsable {@code double}.
 536      * @see    java.lang.Double#valueOf(String)
 537      * @since 1.2
 538      */
 539     public static double parseDouble(String s) throws NumberFormatException {
 540         return FloatingDecimal.readJavaFormatString(s).doubleValue();
 541     }
 542 
 543     /**
 544      * Returns {@code true} if the specified number is a
 545      * Not-a-Number (NaN) value, {@code false} otherwise.
 546      *
 547      * @param   v   the value to be tested.
 548      * @return  {@code true} if the value of the argument is NaN;
 549      *          {@code false} otherwise.
 550      */
 551     static public boolean isNaN(double v) {
 552         return (v != v);
 553     }
 554 
 555     /**
 556      * Returns {@code true} if the specified number is infinitely
 557      * large in magnitude, {@code false} otherwise.
 558      *
 559      * @param   v   the value to be tested.
 560      * @return  {@code true} if the value of the argument is positive
 561      *          infinity or negative infinity; {@code false} otherwise.
 562      */
 563     static public boolean isInfinite(double v) {
 564         return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
 565     }
 566 
 567     /**














 568      * The value of the Double.
 569      *
 570      * @serial
 571      */
 572     private final double value;
 573 
 574     /**
 575      * Constructs a newly allocated {@code Double} object that
 576      * represents the primitive {@code double} argument.
 577      *
 578      * @param   value   the value to be represented by the {@code Double}.
 579      */
 580     public Double(double value) {
 581         this.value = value;
 582     }
 583 
 584     /**
 585      * Constructs a newly allocated {@code Double} object that
 586      * represents the floating-point value of type {@code double}
 587      * represented by the string. The string is converted to a




 259      * <tr><td>{@code Double.MAX_VALUE}</td>
 260      *     <td>{@code 0x1.fffffffffffffp1023}</td>
 261      * <tr><td>{@code Minimum Normal Value}</td>
 262      *     <td>{@code 0x1.0p-1022}</td>
 263      * <tr><td>{@code Maximum Subnormal Value}</td>
 264      *     <td>{@code 0x0.fffffffffffffp-1022}</td>
 265      * <tr><td>{@code Double.MIN_VALUE}</td>
 266      *     <td>{@code 0x0.0000000000001p-1022}</td>
 267      * </table>
 268      * @param   d   the {@code double} to be converted.
 269      * @return a hex string representation of the argument.
 270      * @since 1.5
 271      * @author Joseph D. Darcy
 272      */
 273     public static String toHexString(double d) {
 274         /*
 275          * Modeled after the "a" conversion specifier in C99, section
 276          * 7.19.6.1; however, the output of this method is more
 277          * tightly specified.
 278          */
 279         if (!isFinite(d) )
 280             // For infinity and NaN, use the decimal output.
 281             return Double.toString(d);
 282         else {
 283             // Initialized to maximum size of output.
 284             StringBuffer answer = new StringBuffer(24);
 285 
 286             if (Math.copySign(1.0, d) == -1.0)    // value is negative,
 287                 answer.append("-");                  // so append sign info
 288 
 289             answer.append("0x");
 290 
 291             d = Math.abs(d);
 292 
 293             if(d == 0.0) {
 294                 answer.append("0.0p0");
 295             }
 296             else {
 297                 boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
 298 
 299                 // Isolate significand bits and OR in a high-order bit


 531      * @return the {@code double} value represented by the string
 532      *         argument.
 533      * @throws NullPointerException  if the string is null
 534      * @throws NumberFormatException if the string does not contain
 535      *         a parsable {@code double}.
 536      * @see    java.lang.Double#valueOf(String)
 537      * @since 1.2
 538      */
 539     public static double parseDouble(String s) throws NumberFormatException {
 540         return FloatingDecimal.readJavaFormatString(s).doubleValue();
 541     }
 542 
 543     /**
 544      * Returns {@code true} if the specified number is a
 545      * Not-a-Number (NaN) value, {@code false} otherwise.
 546      *
 547      * @param   v   the value to be tested.
 548      * @return  {@code true} if the value of the argument is NaN;
 549      *          {@code false} otherwise.
 550      */
 551     public static boolean isNaN(double v) {
 552         return (v != v);
 553     }
 554 
 555     /**
 556      * Returns {@code true} if the specified number is infinitely
 557      * large in magnitude, {@code false} otherwise.
 558      *
 559      * @param   v   the value to be tested.
 560      * @return  {@code true} if the value of the argument is positive
 561      *          infinity or negative infinity; {@code false} otherwise.
 562      */
 563     public static boolean isInfinite(double v) {
 564         return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
 565     }
 566 
 567     /**
 568      * Returns {@code true} if the argument is a finite floating-point
 569      * value; returns {@code false} otherwise (for NaN and infinity
 570      * arguments).
 571      *
 572      * @param d the {@code double} value to be tested
 573      * @return {@code true} if the argument is a finite
 574      * floating-point value, {@code false} otherwise.
 575      * @since 1.8
 576      */
 577     public static boolean isFinite(double d) {
 578         return Math.abs(d) <= DoubleConsts.MAX_VALUE;
 579     }
 580 
 581     /**
 582      * The value of the Double.
 583      *
 584      * @serial
 585      */
 586     private final double value;
 587 
 588     /**
 589      * Constructs a newly allocated {@code Double} object that
 590      * represents the primitive {@code double} argument.
 591      *
 592      * @param   value   the value to be represented by the {@code Double}.
 593      */
 594     public Double(double value) {
 595         this.value = value;
 596     }
 597 
 598     /**
 599      * Constructs a newly allocated {@code Double} object that
 600      * represents the floating-point value of type {@code double}
 601      * represented by the string. The string is converted to a