栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在C#中从double中提取尾数和指数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在C#中从double中提取尾数和指数

二进制格式不应该更改-对现有规范肯定是一个重大更改。正如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++;    }    …}

当时的评论对我来说很有意义,但是我敢肯定我现在必须考虑一下。在第一部分之后,您将获得“原始”指数和尾数-其余代码仅有助于以一种更简单的方式对待它们。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465392.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号