简体   繁体   English

为什么 C# 的十进制使用二进制 integer 有效位?

[英]Why does C#'s decimal use binary integer significand?

The new IEEE 128-bit decimal floating-point type https://en.wikipedia.org/wiki/Decimal128_floating-point_format specifies that the significand (mantissa) can be represented in one of two ways, either as a simple binary integer, or in densely packed decimal (in which case every ten bits represent three decimal digits).新的 IEEE 128 位十进制浮点类型https://en.wikipedia.org/wiki/Decimal128_floating-point_format指定有效位(尾数)可以用两种方式之一表示,或者作为简单的二进制 Z157DB7DF530023575515D366C9B672 或 E以密集十进制表示(在这种情况下,每十位代表三个十进制数字)。

C#'s decimal type predates this standard, but has the same idea. C# 的decimal类型早于该标准,但具有相同的想法。 It went with binary integer significand.它带有二进制 integer 有效位。

On the face of it, this seems inefficient;从表面上看,这似乎效率低下。 for addition and subtraction, to line up the significands, you have to divide one of them by a power of ten;对于加法和减法,要排列有效数字,您必须将其中一个除以十的幂; and division is the most expensive of all the arithmetic operators.除法是所有算术运算符中最昂贵的。

What was the reason for the choice?选择的原因是什么? What corresponding advantage was considered worth that penalty?什么相应的优势被认为值得惩罚?

Choosing one representation is almost always about trade offs.选择一种表示几乎总是关于权衡。

From here这里

A binary encoding is inherently less efficient for conversions to or from decimal-encoded data, such as strings (ASCII, Unicode, etc.) and BCD.二进制编码在与十进制编码数据(如字符串(ASCII、Unicode 等)和 BCD 等)之间进行转换的效率本来就较低。 A binary encoding is therefore best chosen only when the data are binary rather than decimal.因此,仅当数据是二进制而不是十进制时才最好选择二进制编码。 IBM has published some unverified performance data. IBM 发布了一些未经验证的性能数据。

Here you can find more about the relative performance.在这里您可以找到有关相对性能的更多信息。

Basically, it asserts your thoughts, that decimal significant is generally faster, but most operations show similar performance and binary even winning in division.基本上,它肯定了你的想法,小数位通常更快,但大多数运算显示出相似的性能,二进制甚至在除法中获胜。 Also keep in mind, since Intel mostly seems to rely on binary significants (i couldn't find hints about other manufactures), they are more likely to get hardware support and than might beat decimals by a good margin.还请记住,由于英特尔似乎主要依赖于二进制显着性(我找不到有关其他制造商的提示),因此它们更有可能获得硬件支持,并且可能会大大超过小数点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM