/[classpath]/classpath/java/lang/Double.java
ViewVC logotype

Diff of /classpath/java/lang/Double.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.40 by tromey, Sun Sep 18 23:00:24 2005 UTC revision 1.41 by tromey, Wed Dec 7 20:16:32 2005 UTC
# Line 173  public final class Double extends Number Line 173  public final class Double extends Number
173    }    }
174    
175    /**    /**
176       * Convert a double value to a hexadecimal string.  This converts as
177       * follows:
178       * <ul>
179       * <li> A NaN value is converted to the string "NaN".
180       * <li> Positive infinity is converted to the string "Infinity".
181       * <li> Negative infinity is converted to the string "-Infinity".
182       * <li> For all other values, the first character of the result is '-'
183       * if the value is negative.  This is followed by '0x1.' if the
184       * value is normal, and '0x0.' if the value is denormal.  This is
185       * then followed by a (lower-case) hexadecimal representation of the
186       * mantissa, with leading zeros as required for denormal values.
187       * The next character is a 'p', and this is followed by a decimal
188       * representation of the unbiased exponent.
189       * </ul>
190       * @param d the double value
191       * @return the hexadecimal string representation
192       * @since 1.5
193       */
194      public static String toHexString(double d)
195      {
196        if (isNaN(d))
197          return "NaN";
198        if (isInfinite(d))
199          return d < 0 ? "-Infinity" : "Infinity";
200    
201        long bits = doubleToLongBits(d);
202        StringBuilder result = new StringBuilder();
203        
204        if (bits < 0)
205          result.append('-');
206        result.append("0x");
207    
208        final int mantissaBits = 52;
209        final int exponentBits = 11;
210        long mantMask = (1L << mantissaBits) - 1;
211        long mantissa = bits & mantMask;
212        long expMask = (1L << exponentBits) - 1;
213        long exponent = (bits >>> mantissaBits) & expMask;
214    
215        result.append(exponent == 0 ? '0' : '1');
216        result.append('.');
217        result.append(Long.toHexString(mantissa));
218        if (exponent == 0 && mantissa != 0)
219          {
220            // Treat denormal specially by inserting '0's to make
221            // the length come out right.  The constants here are
222            // to account for things like the '0x'.
223            int offset = 4 + ((bits < 0) ? 1 : 0);
224            // The silly +3 is here to keep the code the same between
225            // the Float and Double cases.  In Float the value is
226            // not a multiple of 4.
227            int desiredLength = offset + (mantissaBits + 3) / 4;
228            while (result.length() < desiredLength)
229              result.insert(offset, '0');
230          }
231        result.append('p');
232        if (exponent == 0 && mantissa == 0)
233          {
234            // Zero, so do nothing special.
235          }
236        else
237          {
238            // Apply bias.
239            boolean denormal = exponent == 0;
240            exponent -= (1 << (exponentBits - 1)) - 1;
241            // Handle denormal.
242            if (denormal)
243              ++exponent;
244          }
245    
246        result.append(Long.toString(exponent));
247        return result.toString();
248      }
249    
250      /**
251     * Returns a <code>Double</code> object wrapping the value.     * Returns a <code>Double</code> object wrapping the value.
252     * In contrast to the <code>Double</code> constructor, this method     * In contrast to the <code>Double</code> constructor, this method
253     * may cache some values.  It is used by boxing conversion.     * may cache some values.  It is used by boxing conversion.

Legend:
Removed from v.1.40  
changed lines
  Added in v.1.41

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26