c# - 为什么 C# System.Decimal (decimal) "waste"位?

标签 c# data-structures decimal cpu-architecture internals

official docs 中所写System.Decimal 的 128 位是这样填充的:

The return value is a four-element array of 32-bit signed integers.

The first, second, and third elements of the returned array contain the low, middle, and high 32 bits of the 96-bit integer number.

The fourth element of the returned array contains the scale factor and sign. It consists of the following parts:

Bits 0 to 15, the lower word, are unused and must be zero.

Bits 16 to 23 must contain an exponent between 0 and 28, which indicates the power of 10 to divide the integer number.

Bits 24 to 30 are unused and must be zero.

Bit 31 contains the sign: 0 mean positive, and 1 means negative.


考虑到这一点,人们可以看到有些位被“浪费”或未使用。
为什么不是例如 120 位整数、7 位指数和 1 位符号。
十进制可能有一个很好的理由。这个问题想知道这个决定背后的原因。

最佳答案

基于 Kevin Gosse 的评论

For what it's worth, the decimal type seems to predate .net. The .net framework CLR delegates the computations to the oleaut32 lib, and I could find traces of the DECIMAL type as far back as Windows 95


我进一步搜索并在 oleauth32 Windows 95 中找到了可能使用 DECIMAL 代码的用户。
旧的 Visual Basic(非基于 .NET)和 VBA 有一种称为“Variant”的动态类型。在那里(并且仅在那里)您可以保存与我们当前几乎相同的东西 System.Decimal .
Variant 总是 128 位,带有 为枚举值保留的前 16 位 其中数据类型在 Variant 内。
其余 112 位的分离可能基于 90 年代早期的通用 CPU 架构或 Windows 程序员的易用性。不打包指数并 checkin 一个字节只是为了让整数多一个字节,这听起来很明智。
当 .NET 构建此类型的现有(低级)代码时,它的操作被重用于 System.Decimal .
这一切都没有经过 100% 验证,我希望答案包含更多历史证据,但这就是我可以拼凑的东西。

关于c# - 为什么 C# System.Decimal (decimal) "waste"位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62899333/

相关文章:

javascript - 如何在 JavaScript 中进行乘法运算?小数的问题

c# - 在 .net 站点中使用资源文件的最佳替代方法

java - 字符串堆未正确插入

ios - 获取小数点后一位 iOS Objective C

javascript - 将 JavaScript 字符串变量转换为小数/货币

c# - 简单的csv阅读器?

c# - 单列中的多行

c# - System.Windows.Media.Imaging.WriteableBitmap 泄漏内存?

c# - 打开文件时出现 FileNotFound 异常

java - Java中的Dijkstra算法