.NET 十进制的二进制表示 [英] Binary representation of a .NET Decimal

查看:35
本文介绍了.NET 十进制的二进制表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET decimal 类型如何在内存中以二进制形式表示?

How does a .NET decimal type get represented in binary in memory?

我们都知道浮点数是如何存储的,也知道浮点数不准确的原因,但除了以下内容,我找不到任何关于 decimal 的信息:

We all know how floating-point numbers are stored and the thusly the reasons for the inaccuracy thereof, but I can't find any information about decimal except the following:

  1. 显然比浮点数更准确
  2. 占用 128 位内存
  3. 2^96 + 符号范围
  4. 28(有时是 29?)总有效位数

有什么办法可以解决这个问题吗?我内心的计算机科学家要求答案,经过一个小时的尝试研究,我找不到答案.似乎要么有很多浪费的位,要么我只是在脑海中想象这个错误.有人能解释一下吗?

Is there any way I can figure this out? The computer scientist in me demands the answer and after an hour of attempted research, I cannot find it. It seems like there's either a lot of wasted bits or I'm just picturing this wrong in my head. Can anyone shed some light on this please?

推荐答案

Decimal.GetBits 获取您想要的信息.

Decimal.GetBits for the information you want.

基本上它是一个 96 位整数作为尾数,加上一个符号位,再加上一个指数,表示将其向右移动多少个小数位.

Basically it's a 96 bit integer as the mantissa, plus a sign bit, plus an exponent to say how many decimal places to shift it to the right.

因此,要表示 3.261,您的尾数为 3261,符号位为 0(即正数),指数为 3.请注意,十进制没有(故意)标准化,因此您可以 例如,使用尾数 32610 和指数 4 来表示 3.2610.

So to represent 3.261 you'd have a mantissa of 3261, a sign bit of 0 (i.e. positive), and an exponent of 3. Note that decimal isn't normalized (deliberately) so you can also represent 3.2610 by using a mantissa of 32610 and an exponent of 4, for example.

我在关于十进制浮点数的文章中有更多信息.

I have some more information in my article on decimal floating point.

这篇关于.NET 十进制的二进制表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆