为什么numpy.power对于小指数返回0,而math.pow返回正确答案? [英] Why does numpy.power return 0 for small exponents while math.pow returns the correct answer?

查看:138
本文介绍了为什么numpy.power对于小指数返回0,而math.pow返回正确答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In [25]: np.power(10,-100)
Out[25]: 0

In [26]: math.pow(10,-100)
Out[26]: 1e-100

我希望两个命令都返回1e-100.这也不是一个精度问题,因为即使将精度提高到500后问题仍然存在.是否可以更改某些设置以获得正确答案?

I would expect both the commands to return 1e-100. This is not a precision issue either, since the issue persists even after increasing precision to 500. Is there some setting which I can change to get the correct answer?

推荐答案

哦,比这更糟糕":

In [2]: numpy.power(10,-1)   
Out[2]: 0

但这暗示了正在发生的事情:10是整数,而numpy.power不会将数字强制转换为浮点数.但这有效:

But this is a hint to what's going on: 10 is an integer, and numpy.power doesn't coerce the numbers to floats. But this works:

In [3]: numpy.power(10.,-1)
Out[3]: 0.10000000000000001

In [4]: numpy.power(10.,-100)
Out[4]: 1e-100

但是请注意,幂运算符**确实会转换为浮点数:

Note, however, that the power operator, **, does convert to float:

In [5]: 10**-1
Out[5]: 0.1

这篇关于为什么numpy.power对于小指数返回0,而math.pow返回正确答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆