`Decimal`类型有最大精度吗?

huangapple go评论61阅读模式
英文:

Does the `Decimal` type have a maximum precision?

问题

我认为Decimal类型的整个重点是任意精度算术。(或者更确切地说,我认为Decimal支持任意精度以及支持十进制算术。)

然而,我在查看某人的问题时遇到的一个示例使我相信它确实有限制。考虑以下代码:

let string = "728509129536673284379577474947011174006"

if var decimal = Decimal(string: string) {
    print("string \n'\(string) as a Decimal = \n'\(String(describing:decimal))'")
} else {
    print("error converting '\(string)' to a decimal")
}

这会输出:

string 
'728509129536673284379577474947011174006 as a Decimal = 
'728509129536673284379577474947011174000'

看起来最后一位数字丢失了。我尝试了各种其他值,它们都在最后一位数字处开始显示零(看起来当值包含39位小数位时,低位数字会被截断为零)。这是否在某处有文档记录?

英文:

I thought the whole point of the Decimal type was arbitrary precision arithmetic. (Or rather I thought that Decimal supported arbitrary precision as well as supporting base-10 arithmetic.)

However, an example I ran into while looking at somebody's question lead me to believe that it does have a limit. Consider this code:

let string = "728509129536673284379577474947011174006"

if var decimal = Decimal(string: string) {
    print("string \n'\(string) as a Decimal = \n'\(String(describing:decimal))'")
} else {
    print("error converting '\(string)' to a decimal")
}

That outputs

string 
'728509129536673284379577474947011174006 as a Decimal = 
'728509129536673284379577474947011174000'

It looks like the last digit gets lost. I tried various other values, and they all start showing zeros at that last digit (It looks like the low-order digit gets truncated to zero when the value contains 39 decimal digits.)

Is that documented somewhere?

答案1

得分: 3

DecimalNSDecimalNumber 的桥接版本。

> 用于表示和执行十进制数字上的算术运算的对象,可桥接到 Decimal;...

它的表示方式也在 NSDecimalNumber 中有文档记录:

> 一个实例可以表示任何可以表示为 mantissa x 10^exponent 的数字,其中 mantissa 是一个最多38位十进制整数,而 exponent 是从 -128 到 127 的整数。

这有点不准确,因为 _mantissa 属性实际上并不存储38位十进制数字,而是128位。

无论如何,您可以说 Decimal 的“最大精度”为38位(或者有时根据尾数为39位),就像 Double 的“最大精度”为15位一样(或者有时根据尾数为16位)。

当您尝试解析比 Decimal 能处理的更精确的数字时,您会发现精度丢失:

Decimal(string: "3402823669209384634633746074317682114551") // 40位数字
// 结果为    3402823669209384634633746074317682114550
英文:

Decimal is the bridged version of NSDecimalNumber.

> An object for representing and performing arithmetic on base-10 numbers that bridges to Decimal; ...

Its representation is also documented in NSDecimalNumber:

> An instance can represent any number that can be expressed as mantissa x 10^exponent where mantissa is a decimal integer up to 38 digits long, and exponent is an integer from –128 through 127.

That is a bit inaccurate, since the _mantissa property does not actually store 38 decimal digits, but 128 bits.

In any case, you could say that the "maximum precision" of Decimal is 38 digits (or sometimes 39 digits depending on the mantissa), in the same sense that Double has a "maximum precision" of 15 digits (or sometimes 16 digits depending on the mantissa).

When you try to parse a number that is more precise than Decimal can handle, you can see that the precision is lost:

Decimal(string: "3402823669209384634633746074317682114551") // 40 digits
// results in    3402823669209384634633746074317682114550

huangapple
  • 本文由 发表于 2023年7月23日 21:56:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定