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

Print this page


   1 /*
   2  * Copyright (c) 1994, 2012, 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


 272      *     <td>{@code 0x0.fffffffffffffp-1022}</td>
 273      * <tr><td>{@code Double.MIN_VALUE}</td>
 274      *     <td>{@code 0x0.0000000000001p-1022}</td>
 275      * </table>
 276      * @param   d   the {@code double} to be converted.
 277      * @return a hex string representation of the argument.
 278      * @since 1.5
 279      * @author Joseph D. Darcy
 280      */
 281     public static String toHexString(double d) {
 282         /*
 283          * Modeled after the "a" conversion specifier in C99, section
 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             StringBuffer answer = new StringBuffer(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             }
 304             else {
 305                 boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
 306 
 307                 // Isolate significand bits and OR in a high-order bit
 308                 // so that the string representation has a known
 309                 // length.
 310                 long signifBits = (Double.doubleToLongBits(d)
 311                                    & DoubleConsts.SIGNIF_BIT_MASK) |
 312                     0x1000000000000000L;
 313 
 314                 // Subnormal values have a 0 implicit bit; normal
 315                 // values have a 1 implicit bit.
 316                 answer.append(subnormal ? "0." : "1.");
 317 
 318                 // Isolate the low-order 13 digits of the hex
 319                 // representation.  If all the digits are zero,
 320                 // replace with a single 0; otherwise, remove all
 321                 // trailing zeros.
 322                 String signif = Long.toHexString(signifBits).substring(3,16);
 323                 answer.append(signif.equals("0000000000000") ? // 13 zeros
 324                               "0":
 325                               signif.replaceFirst("0{1,12}$", ""));
 326 

 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("p" + (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:
 353      *


   1 /*
   2  * Copyright (c) 1994, 2013, 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


 272      *     <td>{@code 0x0.fffffffffffffp-1022}</td>
 273      * <tr><td>{@code Double.MIN_VALUE}</td>
 274      *     <td>{@code 0x0.0000000000001p-1022}</td>
 275      * </table>
 276      * @param   d   the {@code double} to be converted.
 277      * @return a hex string representation of the argument.
 278      * @since 1.5
 279      * @author Joseph D. Darcy
 280      */
 281     public static String toHexString(double d) {
 282         /*
 283          * Modeled after the "a" conversion specifier in C99, section
 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:
 353      *