caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 7 | |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 8 | #if 0 |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 9 | // snippets that one day may be useful, unused for now... |
| 10 | |
| 11 | // get sign, exponent, mantissa from double |
| 12 | // Translate the double into sign, exponent and mantissa. |
| 13 | long bits = BitConverter.DoubleToInt64Bits(d); |
| 14 | // Note that the shift is sign-extended, hence the test against -1 not 1 |
| 15 | bool negative = (bits < 0); |
| 16 | int exponent = (int) ((bits >> 52) & 0x7ffL); |
| 17 | long mantissa = bits & 0xfffffffffffffL; |
| 18 | |
| 19 | // Subnormal numbers; exponent is effectively one higher, |
| 20 | // but there's no extra normalisation bit in the mantissa |
| 21 | if (exponent==0) |
| 22 | { |
| 23 | exponent++; |
| 24 | } |
| 25 | // Normal numbers; leave exponent as it is but add extra |
| 26 | // bit to the front of the mantissa |
| 27 | else |
| 28 | { |
| 29 | mantissa = mantissa | (1L<<52); |
| 30 | } |
| 31 | |
| 32 | // Bias the exponent. It's actually biased by 1023, but we're |
| 33 | // treating the mantissa as m.0 rather than 0.m, so we need |
| 34 | // to subtract another 52 from it. |
| 35 | exponent -= 1075; |
| 36 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 37 | if (mantissa == 0) |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 38 | { |
| 39 | return "0"; |
| 40 | } |
| 41 | |
| 42 | /* Normalize */ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 43 | while((mantissa & 1) == 0) |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 44 | { /* i.e., Mantissa is even */ |
| 45 | mantissa >>= 1; |
| 46 | exponent++; |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 47 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 48 | #endif |