< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 284          * 7.19.6.1; however, the output of this method is more
 285          * tightly specified.
 286          */
 287         if (!isFinite(d) )
 288             // For infinity and NaN, use the decimal output.
 289             return Double.toString(d);
 290         else {
 291             // Initialized to maximum size of output.
 292             StringBuilder answer = new StringBuilder(24);
 293 
 294             if (Math.copySign(1.0, d) == -1.0)    // value is negative,
 295                 answer.append("-");                  // so append sign info
 296 
 297             answer.append("0x");
 298 
 299             d = Math.abs(d);
 300 
 301             if(d == 0.0) {
 302                 answer.append("0.0p0");
 303             } else {
 304                 boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
 305 
 306                 // Isolate significand bits and OR in a high-order bit
 307                 // so that the string representation has a known
 308                 // length.
 309                 long signifBits = (Double.doubleToLongBits(d)
 310                                    & DoubleConsts.SIGNIF_BIT_MASK) |
 311                     0x1000000000000000L;
 312 
 313                 // Subnormal values have a 0 implicit bit; normal
 314                 // values have a 1 implicit bit.
 315                 answer.append(subnormal ? "0." : "1.");
 316 
 317                 // Isolate the low-order 13 digits of the hex
 318                 // representation.  If all the digits are zero,
 319                 // replace with a single 0; otherwise, remove all
 320                 // trailing zeros.
 321                 String signif = Long.toHexString(signifBits).substring(3,16);
 322                 answer.append(signif.equals("0000000000000") ? // 13 zeros
 323                               "0":
 324                               signif.replaceFirst("0{1,12}$", ""));
 325 
 326                 answer.append('p');
 327                 // If the value is subnormal, use the E_min exponent
 328                 // value for double; otherwise, extract and report d's
 329                 // exponent (the representation of a subnormal uses
 330                 // E_min -1).
 331                 answer.append(subnormal ?
 332                               DoubleConsts.MIN_EXPONENT:
 333                               Math.getExponent(d));
 334             }
 335             return answer.toString();
 336         }
 337     }
 338 
 339     /**
 340      * Returns a {@code Double} object holding the
 341      * {@code double} value represented by the argument string
 342      * {@code s}.
 343      *
 344      * <p>If {@code s} is {@code null}, then a
 345      * {@code NullPointerException} is thrown.
 346      *
 347      * <p>Leading and trailing whitespace characters in {@code s}
 348      * are ignored.  Whitespace is removed as if by the {@link
 349      * String#trim} method; that is, both ASCII space and control
 350      * characters are removed. The rest of {@code s} should
 351      * constitute a <i>FloatValue</i> as described by the lexical
 352      * syntax rules:


 557      *
 558      * @param   v   the value to be tested.
 559      * @return  {@code true} if the value of the argument is positive
 560      *          infinity or negative infinity; {@code false} otherwise.
 561      */
 562     public static boolean isInfinite(double v) {
 563         return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
 564     }
 565 
 566     /**
 567      * Returns {@code true} if the argument is a finite floating-point
 568      * value; returns {@code false} otherwise (for NaN and infinity
 569      * arguments).
 570      *
 571      * @param d the {@code double} value to be tested
 572      * @return {@code true} if the argument is a finite
 573      * floating-point value, {@code false} otherwise.
 574      * @since 1.8
 575      */
 576     public static boolean isFinite(double d) {
 577         return Math.abs(d) <= DoubleConsts.MAX_VALUE;
 578     }
 579 
 580     /**
 581      * The value of the Double.
 582      *
 583      * @serial
 584      */
 585     private final double value;
 586 
 587     /**
 588      * Constructs a newly allocated {@code Double} object that
 589      * represents the primitive {@code double} argument.
 590      *
 591      * @param   value   the value to be represented by the {@code Double}.
 592      */
 593     public Double(double value) {
 594         this.value = value;
 595     }
 596 
 597     /**


   1 /*
   2  * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 284          * 7.19.6.1; however, the output of this method is more
 285          * tightly specified.
 286          */
 287         if (!isFinite(d) )
 288             // For infinity and NaN, use the decimal output.
 289             return Double.toString(d);
 290         else {
 291             // Initialized to maximum size of output.
 292             StringBuilder answer = new StringBuilder(24);
 293 
 294             if (Math.copySign(1.0, d) == -1.0)    // value is negative,
 295                 answer.append("-");                  // so append sign info
 296 
 297             answer.append("0x");
 298 
 299             d = Math.abs(d);
 300 
 301             if(d == 0.0) {
 302                 answer.append("0.0p0");
 303             } else {
 304                 boolean subnormal = (d < Double.MIN_NORMAL);
 305 
 306                 // Isolate significand bits and OR in a high-order bit
 307                 // so that the string representation has a known
 308                 // length.
 309                 long signifBits = (Double.doubleToLongBits(d)
 310                                    & DoubleConsts.SIGNIF_BIT_MASK) |
 311                     0x1000000000000000L;
 312 
 313                 // Subnormal values have a 0 implicit bit; normal
 314                 // values have a 1 implicit bit.
 315                 answer.append(subnormal ? "0." : "1.");
 316 
 317                 // Isolate the low-order 13 digits of the hex
 318                 // representation.  If all the digits are zero,
 319                 // replace with a single 0; otherwise, remove all
 320                 // trailing zeros.
 321                 String signif = Long.toHexString(signifBits).substring(3,16);
 322                 answer.append(signif.equals("0000000000000") ? // 13 zeros
 323                               "0":
 324                               signif.replaceFirst("0{1,12}$", ""));
 325 
 326                 answer.append('p');
 327                 // If the value is subnormal, use the E_min exponent
 328                 // value for double; otherwise, extract and report d's
 329                 // exponent (the representation of a subnormal uses
 330                 // E_min -1).
 331                 answer.append(subnormal ?
 332                               Double.MIN_EXPONENT:
 333                               Math.getExponent(d));
 334             }
 335             return answer.toString();
 336         }
 337     }
 338 
 339     /**
 340      * Returns a {@code Double} object holding the
 341      * {@code double} value represented by the argument string
 342      * {@code s}.
 343      *
 344      * <p>If {@code s} is {@code null}, then a
 345      * {@code NullPointerException} is thrown.
 346      *
 347      * <p>Leading and trailing whitespace characters in {@code s}
 348      * are ignored.  Whitespace is removed as if by the {@link
 349      * String#trim} method; that is, both ASCII space and control
 350      * characters are removed. The rest of {@code s} should
 351      * constitute a <i>FloatValue</i> as described by the lexical
 352      * syntax rules:


 557      *
 558      * @param   v   the value to be tested.
 559      * @return  {@code true} if the value of the argument is positive
 560      *          infinity or negative infinity; {@code false} otherwise.
 561      */
 562     public static boolean isInfinite(double v) {
 563         return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
 564     }
 565 
 566     /**
 567      * Returns {@code true} if the argument is a finite floating-point
 568      * value; returns {@code false} otherwise (for NaN and infinity
 569      * arguments).
 570      *
 571      * @param d the {@code double} value to be tested
 572      * @return {@code true} if the argument is a finite
 573      * floating-point value, {@code false} otherwise.
 574      * @since 1.8
 575      */
 576     public static boolean isFinite(double d) {
 577         return Math.abs(d) <= Double.MAX_VALUE;
 578     }
 579 
 580     /**
 581      * The value of the Double.
 582      *
 583      * @serial
 584      */
 585     private final double value;
 586 
 587     /**
 588      * Constructs a newly allocated {@code Double} object that
 589      * represents the primitive {@code double} argument.
 590      *
 591      * @param   value   the value to be represented by the {@code Double}.
 592      */
 593     public Double(double value) {
 594         this.value = value;
 595     }
 596 
 597     /**


< prev index next >