为什么我能够成功 return 一个大的 np.float128 但是当我尝试在 numpy 中创建相同的 np.float128 时我只是得到 inf?

Why is that I'm able to successfully return a large np.float128 but when I try to create the same np.float128 in numpy I just get inf?

我正在编写一个函数来获取大数的指数:

def exp_large_float(x):
    return np.exp(np.float128(np.array([x])))

result = exp_large_float(800.)
print(result)
>>> [2.72637457e+347]

print(result.dtype)
>>> float128

我正在测试 result 是我想要的,所以我想创建一个 "ground truth" 数组并将其与 exp_large_float:

的输出进行比较
ground_truth = np.float128(2.72637457e+347)
print(ground_truth)
>>> inf

为什么我能够成功 return np.float128 of 2.72637457e+347 但是当我尝试在 numpy 中创建同样的东西时我只是得到 inf?

我正在使用 python 版本 2.7.15。

正如@Warren Weckesser 所指出的,您的代码无法正常工作,因为

2.72637457e+347

是 Python 中的 float 文字,因此它被解释为标准(大概是 64 位)float 并变为 inf before 它被传递给float128工厂。

您可以通过将参数作为字符串传递来避免这种情况。

ground_truth = np.float128("2.72637457e+347")