C# 中的十进制类型与 IEEE-754 标准

发布于 2025-01-01 13:40:39 字数 200 浏览 1 评论 0原文

C# 中的 Decimal 类型是否遵循经典双精度表示(IEEE-754 标准)的相同规则(公式、标准化/非标准化、隐含 1、指数偏差),除了使用基数 10 而不是基数 2。

隐含使用基数是什么意思10 而不是基数 2 ?

是否存在像“IEEE-754 double”一样的相同行为,即即使对于有限精度(例如 28/29 位),相邻值之间也存在一些间隙?

Is Decimal type in C# follow the same rules (formula,normalized/denormalized,implied 1,Exponent bias) of classic double representation (IEEE-754 standard) except the use of base 10 instead of base 2.

What does implied the use of base 10 instead of base 2 ?

Is there, like "IEEE-754 double", the same behavior namely some gaps between adjacent values even for a finite precision (like 28/29 digits)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2025-01-08 13:40:39

不,C# 十进制 不遵循与 IEEE 754 浮点数相同的规则。 C# 4 规范非常清楚地说明了它的行为方式(第 4.1.7 节):

decimal 类型是一种 128 位数据类型,适用于金融和货币计算。小数类型可以表示范围从 1.0 × 10−28 到大约 7.9 × 1028 的值,有效数字为 28-29 位。

decimal 类型的有限值集的形式为 (–1)s × c × 10-e,其中符号 s 为 0 或 1,系数 c 由下式给出: 0 ≤ c< 296,小数位数 e 为 0 ≤ e ≤ 28。decimal 类型不支持有符号零、无穷大或 NaN。 十进制表示为按十的幂缩放的 96 位整数。对于绝对值小于 1.0m 的小数,该值精确到小数点后第 28 位,但不再精确。对于绝对值大于或等于1.0m小数,该值精确到28或29位。与 floatdouble 数据类型相反,十进制小数(例如 0.1)可以用 decimal 表示法精确表示。在 floatdouble 表示中,此类数字通常是无限分数,使得这些表示更容易出现舍入错误。

No, C# decimal doesn't follow the same rules as IEEE 754 floating point numbers. The C# 4 specification is quite clear on how it should behave (§4.1.7):

The decimal type is a 128-bit data type suitable for financial and monetary calculations. The decimal type can represent values ranging from 1.0 × 10−28 to approximately 7.9 × 1028 with 28-29 significant digits.

The finite set of values of type decimal are of the form (–1)s × c × 10-e, where the sign s is 0 or 1, the coefficient c is given by 0 ≤ c < 296, and the scale e is such that 0 ≤ e ≤ 28. The decimal type does not support signed zeros, infinities, or NaN's. A decimal is represented as a 96-bit integer scaled by a power of ten. For decimals with an absolute value less than 1.0m, the value is exact to the 28th decimal place, but no further. For decimals with an absolute value greater than or equal to 1.0m, the value is exact to 28 or 29 digits. Contrary to the float and double data types, decimal fractional numbers such as 0.1 can be represented exactly in the decimal representation. In the float and double representations, such numbers are often infinite fractions, making those representations more prone to round-off errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文