blob: d05afdf947c73a8aae0d890cfccdd751bd3d7486 [file] [log] [blame]
caryclark@google.com639df892012-01-10 21:46:10 +00001
2// snippets that one day may be useful, unused for now...
3
4// get sign, exponent, mantissa from double
5// Translate the double into sign, exponent and mantissa.
6 long bits = BitConverter.DoubleToInt64Bits(d);
7 // Note that the shift is sign-extended, hence the test against -1 not 1
8 bool negative = (bits < 0);
9 int exponent = (int) ((bits >> 52) & 0x7ffL);
10 long mantissa = bits & 0xfffffffffffffL;
11
12 // Subnormal numbers; exponent is effectively one higher,
13 // but there's no extra normalisation bit in the mantissa
14 if (exponent==0)
15 {
16 exponent++;
17 }
18 // Normal numbers; leave exponent as it is but add extra
19 // bit to the front of the mantissa
20 else
21 {
22 mantissa = mantissa | (1L<<52);
23 }
24
25 // Bias the exponent. It's actually biased by 1023, but we're
26 // treating the mantissa as m.0 rather than 0.m, so we need
27 // to subtract another 52 from it.
28 exponent -= 1075;
29
30 if (mantissa == 0)
31 {
32 return "0";
33 }
34
35 /* Normalize */
36 while((mantissa & 1) == 0)
37 { /* i.e., Mantissa is even */
38 mantissa >>= 1;
39 exponent++;
40 }