我在python3中使用特殊函数lambertw(k=-1
),我需要将它用于大于/小于最大/最小浮点数(1.7976931348623157e+308
)的数字。你知道吗
我能做什么?你知道吗
我也试过用“十进制”,但没用,例如
from decimal import Decimal
from scipy.special import lambertw
lambertw(Decimal('3.1E+600'))
得到这个
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/share/apps/sistema/Python-3.5.1/lib/python3.5/site-packages/scipy/special/lambertw.py", line 107, in lambertw
return _lambertw(z, k, tol)
TypeError: ufunc '_lambertw' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
SymPy中的^{} 库包含^{} 的实现。
mpmath
实现任意精度浮点运算。你知道吗下面是一个例子。首先,从
sympy
导入mpmath
,并将精度的位数设置为100(选择任意更改以满足您的需要):验证
mpmath
函数是否给出与scipy.special.lambertw
相同的结果:计算
lambertw(3.1e600)
。参数是以字符串形式输入的,因为我们不能将3.1e600表示为常规浮点值。mpmath
将使用前面设置的精度将字符串转换为高精度浮点值。你知道吗我们还可以创建一个变量
x
来保存输入值,然后调用mpmath.lambertw(x)
:结果可以表示为常规浮点值,因此我们将其传递给内置函数
float()
进行转换:decimal
模块应该能够解决您的问题。您遇到的问题可能是没有将精度设置为高于默认值28as mentioned in the docs。要做到这一点,只需调用getcontext().prec = 100
或任何您需要的精度。你知道吗例如,使用您的示例编号,我刚刚运行了以下交互式会话:
相关问题 更多 >
编程相关推荐