math.sin和math.cos中的精度不同

问题描述 投票:2回答:3

我有一个简单的问题来计算python中的辐射的sin和cos。

在我看来,罪限于大于“x.xe-8”的值,而cos能够计算更小的值,例如我的例子中的“x.xe-90”。

有没有办法(除了罪之前的一轮)使罪得以正常工作(当然在其精确限度内)。

>>> import math
>>> math.sin(1.0e-8)
1e-08
>>> math.sin(1.0e-9)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> math.cos(1.0e-90)
1.0

编辑:问题是操作系统。有可用的更新。 (https://www.suse.com/support/update/announcement/2012/suse-ru-20120681-1.html

python math sin cos
3个回答
2
投票

问题是操作系统。 Suse Enterprise 11.2有更新。 - > https://www.suse.com/support/update/announcement/2012/suse-ru-20120681-1.html


0
投票

sin(x) ~ x围绕起源,所以你为什么这么担心呢?减去x**3/6.0必须让你足够接近大多数计算(如果没有,必须添加x ** 5 / 120.0)。


0
投票

我看了一下代码(Python 2.7.5)。你可以在Modules/mathmodule.c找到相关的行:

r = (*func)(x);
PyFPE_END_PROTECT(r);
if (Py_IS_NAN(r)) {
    if (!Py_IS_NAN(x))
        errno = EDOM;
    else
        errno = 0;
}
else if (Py_IS_INFINITY(r)) {
    if (Py_IS_FINITE(x))
        errno = can_overflow ? ERANGE : EDOM;
    else
        errno = 0;
}

func是指向系统sin函数的指针,can_overflow为0.所以你的系统的sin要么返回给定输入的NaN或infinity。

© www.soinside.com 2019 - 2024. All rights reserved.