Converting doubles to Strings

- 109 - JDK float conversion 100 85 270 128 Optimized float conversion 26 30 95 33

5.3.5 Converting doubles to Strings

The double conversion is almost identical to the float conversion, except that the double s extend over a larger range. The differences are the following constants used in place of the corresponding float constants: private static final long doubleSignMask = 0x8000000000000000L; private static final long doubleExpMask = 0x7ff0000000000000L; private static final long doubleFractMask= ~doubleSignMask|doubleExpMask; private static final int doubleExpShift = 52; private static final int doubleExpBias = 1023; private static final double[] d_magnitudes = { as f_magnitudes[] except doubles extending from 1e-323D to 1e308D inclusive ... } The last section of the append method is: int magnitude = magnituded; long i; if magnitude -305 i = long d1E18 d_magnitudes[magnitude + 324]; else i = long d d_magnitudes[magnitude + 323 - 17]; i = i10 = 5 ? i10 + 1 : i10; appendFractDigitss, i, 1; s.appendE; appends,magnitude; and the magnitude methods are: private static int magnitudedouble d { return magnituded,Double.doubleToLongBitsd; } private static int magnitudedouble d, long doubleToLongBits { int magnitude = int doubleToLongBits doubleExpMask doubleExpShift - doubleExpBias 0.301029995663981; if magnitude -323 magnitude = -323; else if magnitude 308 magnitude = 308; if d = d_magnitudes[magnitude+323] { whilemagnitude 309 d = d_magnitudes[magnitude+323] magnitude++; magnitude--; return magnitude; } else { whilemagnitude -324 d d_magnitudes[magnitude+323] - 110 - magnitude--; return magnitude; } } The conversion times compared to the JDK conversions are shown in Table 5-4 . As with float s, formatting double s with the JDK conversion requires additional steps and would consequently take longer, but the method shown here takes even less time, as you normally print fewer digits that require fewer loop iterations. Table 5-4, Time Taken to Append a double to a StringBuffer VM 1.2 1.3 HotSpot 1.0 1.1.6 JDK double conversion 100 92 129 134 Optimized double conversion 16 16 32 23

5.3.6 Converting Objects to Strings