numpy数组元素是否自动舍入?

编程入门 行业动态 更新时间:2024-10-27 15:23:39
本文介绍了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数组元素是否自动舍入?

本文发布于:2023-05-25 13:33:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/306066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   元素   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!