二进制格式不应该更改-对现有规范肯定是一个重大更改。正如Jimmy所说,它被定义为IEEE754 / IEC
60559:1989格式。(C#3.0语言规范第1.3节; ECMA 335第8.2.2节)。DoubleConverter中的代码应该很好并且健壮。
为了将来参考,示例中代码的相关位为:
public static string ToExactString (double d){ … // Translate the double into sign, exponent and mantissa. long bits = BitConverter.DoubleToInt64Bits(d); // Note that the shift is sign-extended, hence the test against -1 not 1 bool negative = (bits & (1L << 63)) != 0; int exponent = (int) ((bits >> 52) & 0x7ffL); long mantissa = bits & 0xfffffffffffffL; // Subnormal numbers; exponent is effectively one higher, // but there's no extra normalisation bit in the mantissa if (exponent==0) { exponent++; } // Normal numbers; leave exponent as it is but add extra // bit to the front of the mantissa else { mantissa = mantissa | (1L << 52); } // Bias the exponent. It's actually biased by 1023, but we're // treating the mantissa as m.0 rather than 0.m, so we need // to subtract another 52 from it. exponent -= 1075; if (mantissa == 0) { return negative ? "-0" : "0"; } while((mantissa & 1) == 0) { mantissa >>= 1; exponent++; } …}
当时的评论对我来说很有意义,但是我敢肯定我现在必须考虑一下。在第一部分之后,您将获得“原始”指数和尾数-其余代码仅有助于以一种更简单的方式对待它们。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)