jdk/src/share/classes/java/lang/Double.java
changeset 15525 0308cc37489b
parent 15311 be0ff4a719bf
child 18143 b6ef7bd945ce
equal deleted inserted replaced
15524:b77777dcac17 15525:0308cc37489b
     1 /*
     1 /*
     2  * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   287         if (!isFinite(d) )
   287         if (!isFinite(d) )
   288             // For infinity and NaN, use the decimal output.
   288             // For infinity and NaN, use the decimal output.
   289             return Double.toString(d);
   289             return Double.toString(d);
   290         else {
   290         else {
   291             // Initialized to maximum size of output.
   291             // Initialized to maximum size of output.
   292             StringBuffer answer = new StringBuffer(24);
   292             StringBuilder answer = new StringBuilder(24);
   293 
   293 
   294             if (Math.copySign(1.0, d) == -1.0)    // value is negative,
   294             if (Math.copySign(1.0, d) == -1.0)    // value is negative,
   295                 answer.append("-");                  // so append sign info
   295                 answer.append("-");                  // so append sign info
   296 
   296 
   297             answer.append("0x");
   297             answer.append("0x");
   298 
   298 
   299             d = Math.abs(d);
   299             d = Math.abs(d);
   300 
   300 
   301             if(d == 0.0) {
   301             if(d == 0.0) {
   302                 answer.append("0.0p0");
   302                 answer.append("0.0p0");
   303             }
   303             } else {
   304             else {
       
   305                 boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
   304                 boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
   306 
   305 
   307                 // Isolate significand bits and OR in a high-order bit
   306                 // Isolate significand bits and OR in a high-order bit
   308                 // so that the string representation has a known
   307                 // so that the string representation has a known
   309                 // length.
   308                 // length.
   322                 String signif = Long.toHexString(signifBits).substring(3,16);
   321                 String signif = Long.toHexString(signifBits).substring(3,16);
   323                 answer.append(signif.equals("0000000000000") ? // 13 zeros
   322                 answer.append(signif.equals("0000000000000") ? // 13 zeros
   324                               "0":
   323                               "0":
   325                               signif.replaceFirst("0{1,12}$", ""));
   324                               signif.replaceFirst("0{1,12}$", ""));
   326 
   325 
       
   326                 answer.append('p');
   327                 // If the value is subnormal, use the E_min exponent
   327                 // If the value is subnormal, use the E_min exponent
   328                 // value for double; otherwise, extract and report d's
   328                 // value for double; otherwise, extract and report d's
   329                 // exponent (the representation of a subnormal uses
   329                 // exponent (the representation of a subnormal uses
   330                 // E_min -1).
   330                 // E_min -1).
   331                 answer.append("p" + (subnormal ?
   331                 answer.append(subnormal ?
   332                                DoubleConsts.MIN_EXPONENT:
   332                               DoubleConsts.MIN_EXPONENT:
   333                                Math.getExponent(d) ));
   333                               Math.getExponent(d));
   334             }
   334             }
   335             return answer.toString();
   335             return answer.toString();
   336         }
   336         }
   337     }
   337     }
   338 
   338