蟒蛇数学,numpy模块不同的结果? [英] python math, numpy modules different results?

查看:157
本文介绍了蟒蛇数学,numpy模块不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到稍微不同的结果计算值的余弦。

 导入数学
math.cos(60.0 / 180.0 *)如何检查这个差异是否在机器精度内? math.pi)
- > 0.5000000000000001

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


解决方案

的差异似乎由格式化例程所引起只有:

 >>> '%.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 的形式实现的,所以在精度上没有实际的差别。


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

解决方案

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.

这篇关于蟒蛇数学,numpy模块不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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