numpy数组元素是否自动舍入? [英] are numpy array elements rounded automatically?

查看:106
本文介绍了numpy数组元素是否自动舍入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有一个浮点数的数组.

I have an numpy array of floats in Python.

当我打印数组时,第一个值是:

When I print the array, the first value is:

[7.14519700e+04, ....

但是,如果我自己打印出第一个值,则打印输出为:

If, however, I print out just the first value on it's own, the print out reads:

71451.9699799

显然,这些数字应该是相同的,所以我只是想知道,数组只是显示元素的舍入形式吗?这里的第二个数字有12位有效数字,第一个只有9位有效数字.

Obviously these numbers should be identical, so I just wondered, is the array just showing me a rounded version of the element? The second number here has 12 significant figures, and the first only has 9.

我想我只是想知道为什么这些数字不同?

I guess I just wonder why these numbers are different?

推荐答案

它只是在打印中,而不是在存储中.唯一的混乱可能是因为第一个示例使用numpy的打印精度设置,第二个示例使用了python的常规打印设置.

It's just in the printing, not in the storage. The only confusion might occur because the first example uses numpy's print precision settings, the second example general python's print settings.

您可以调整numpy的精度并通过

You can adjust the numpy precision and print by

numpy.set_printoptions(precision=20)
print myarray`

(根据您的需要调整精度),或选择标准python格式打印中的有效数字数量:

(adjust precision to your needs), or select the number of significant figures in standard python formatted print:

print ('%.20f' % myarray[0])

数字的内部表示形式始终相同.

The internal representation of the number is always the same.

这篇关于numpy数组元素是否自动舍入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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