Python中的小数模块不准确

发布于 2025-01-17 17:13:20 字数 314 浏览 2 评论 0原文

from decimal import *
Pi=Decimal(3.141592653589793238462643383279502884197169399373)
print(Pi)

实际输出:

3.141592653589793115997963468544185161590576171875

输出应该是:

3.141592653589793238462643383279502884197169399373

为什么值会改变?

from decimal import *
Pi=Decimal(3.141592653589793238462643383279502884197169399373)
print(Pi)

Actual output:

3.141592653589793115997963468544185161590576171875

Output should be:

3.141592653589793238462643383279502884197169399373

Why does the value change?

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

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

发布评论

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

评论(2

青柠芒果 2025-01-24 17:13:20

您正在访问a floating-point number ,and 浮点数本质上是不正确的(另请参阅 python手册)。

要将精确编号传递给小数构造函数,请以字符串的形式传递。

>>> from decimal import Decimal

# bad
>>> Decimal(3.141592653589793238462643383279502884197169399373)
Decimal('3.141592653589793115997963468544185161590576171875')

# good
>>> Decimal('3.141592653589793238462643383279502884197169399373')
Decimal('3.141592653589793238462643383279502884197169399373')

如果您有浮点变量,则可以先将其施加到字符串,然后将其施加到十进制中,以避免一些 floating-probation-point dormision:

>>> a = 0.1 + 0.2
0.30000000000000004
>>> Decimal(a)
Decimal('0.3000000000000000444089209850062616169452667236328125')
>>> Decimal(str(a))
Decimal('0.30000000000000004')
>>>

如果您需要完整的精度,只需一路处理小数:

>>> Decimal("0.1") + Decimal("0.2")
Decimal('0.3')

You're passing in a floating-point number to the Decimal constructor, and floating-point numbers are inherently imprecise (see also the Python manual).

To pass in a precise number to the Decimal constructor, pass it in as a string.

>>> from decimal import Decimal

# bad
>>> Decimal(3.141592653589793238462643383279502884197169399373)
Decimal('3.141592653589793115997963468544185161590576171875')

# good
>>> Decimal('3.141592653589793238462643383279502884197169399373')
Decimal('3.141592653589793238462643383279502884197169399373')

If you have a floating-point variable, you can cast it to a string first, then to a Decimal to avoid some floating-point imprecision:

>>> a = 0.1 + 0.2
0.30000000000000004
>>> Decimal(a)
Decimal('0.3000000000000000444089209850062616169452667236328125')
>>> Decimal(str(a))
Decimal('0.30000000000000004')
>>>

If you need full precision, just work with Decimals all the way:

>>> Decimal("0.1") + Decimal("0.2")
Decimal('0.3')
木有鱼丸 2025-01-24 17:13:20

您应该将字符串传递给 Decimal(),而不是浮点数,浮点数一开始就是不精确的。另外,请注意 Python 文档中的以下内容

与基于硬件的二进制浮点不同,十进制模块具有
用户可更改精度(默认为 28 位),可以为
根据给定问题的需要而大

from decimal import *
getcontext().prec = 100 #precision

pi = Decimal("3.141592653589793238462643383279502884197169399373")
print(pi)    #3.141592653589793238462643383279502884197169399373

You should pass a string to Decimal(), not a float, floats are imprecise to begin with. Also, note the following from the Python docs

Unlike hardware based binary floating point, the decimal module has a
user alterable precision (defaulting to 28 places) which can be as
large as needed for a given problem

from decimal import *
getcontext().prec = 100 #precision

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