python math,numpy模块不同的结果?

发布于 2024-12-10 23:59:11 字数 208 浏览 0 评论 0原文

计算值的余弦时,我得到的结果略有不同。如何检查这种差异是否在机器精度范围内?

import math
math.cos(60.0/180.0*math.pi)
-> 0.5000000000000001

import numpy
numpy.cos(60.0/180.0*numpy.pi)
-> 0.50000000000000011

I get slightly different results calculating the cosine of a value. How can I check that this difference is within machine precision?

import math
math.cos(60.0/180.0*math.pi)
-> 0.5000000000000001

import numpy
numpy.cos(60.0/180.0*numpy.pi)
-> 0.50000000000000011

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

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

发布评论

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

评论(3

擦肩而过的背影 2024-12-17 23:59:11

差异似乎仅由格式化例程引起:

>>> '%.30f' % math.cos(60./180.*math.pi)
'0.500000000000000111022302462516'
>>> '%.30f' % np.cos(60./180.*np.pi)
'0.500000000000000111022302462516'

请注意,np.cos 返回 np.float64 而不是 float,并且显然是该类型默认情况下打印方式不同。在常见硬件上,它们都实现为 64 位 double,因此精度上没有实际差异。

The difference seems to be caused by the formatting routines only:

>>> '%.30f' % math.cos(60./180.*math.pi)
'0.500000000000000111022302462516'
>>> '%.30f' % np.cos(60./180.*np.pi)
'0.500000000000000111022302462516'

Note that np.cos returns np.float64 rather than float, and apparently that type is printed differently by default. On common hardware, they're both implemented as 64-bit double, so there's no actual difference in precision.

风透绣罗衣 2024-12-17 23:59:11

双精度算术可提供 15-16 位十进制有效数字的精度。这两个值与该精度一致。这里没什么好担心的。

请注意,我说“十进制”是为了与双精度值的二进制表示形式中用于有效数的 53 个“二进制”位进行对比。

Double precision arithmetic gives you precision of 15-16 decimal significant figures. These two values agree to that precision. Nothing worry about here.

Note that I say decimal to contrast with the 53 binary bits used for the significand in the binary representation of a double precision value.

蒗幽 2024-12-17 23:59:11

尽管您的数字结果是相等的,但了解如何完全精确地检查它们仍然很有用。有几种方法可以做到这一点:

>>> a = 1.1 + 2.2
>>> b = 3.3
>>> a == b
False
>>> from decimal import Decimal
>>> Decimal.from_float(a)
Decimal('3.300000000000000266453525910037569701671600341796875')
>>> Decimal.from_float(b)
Decimal('3.29999999999999982236431605997495353221893310546875')
>>> a.hex()
'0x1.a666666666667p+1'
>>> b.hex()
'0x1.a666666666666p+1'
>>> a.as_integer_ratio()
(7430939385161319, 2251799813685248)
>>> b.as_integer_ratio()
(3715469692580659, 1125899906842624)

Eventhough your numbers turned out to be equal, it is still useful to know how to examine them at full precision. Here are a couple of ways to do it:

>>> a = 1.1 + 2.2
>>> b = 3.3
>>> a == b
False
>>> from decimal import Decimal
>>> Decimal.from_float(a)
Decimal('3.300000000000000266453525910037569701671600341796875')
>>> Decimal.from_float(b)
Decimal('3.29999999999999982236431605997495353221893310546875')
>>> a.hex()
'0x1.a666666666667p+1'
>>> b.hex()
'0x1.a666666666666p+1'
>>> a.as_integer_ratio()
(7430939385161319, 2251799813685248)
>>> b.as_integer_ratio()
(3715469692580659, 1125899906842624)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文