Python 3 浮点小数点/精度

Python 3 Float Decimal Points/Precision(Python 3 浮点小数点/精度)
本文介绍了Python 3 浮点小数点/精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在读取一个带有浮点数的文本文件,所有这些都带有 1 或 2 个小数点.我正在使用 float() 将一行转换为浮点数,如果失败则引发 ValueError .我将所有浮点数存储在一个列表中.打印出来时,我想将其打印为小数点后 2 位的浮点数.

I am reading a text file with floating point numbers, all with either 1 or 2 decimal points. I am using float() to convert a line into a float, and raising a ValueError if that fails. I am storing all floats in a list. When printing it out, I'd like to print it out as a 2 decimal places floating point.

假设我有一个带有数字 -3,65, 9,17, 1 的文本文件.我阅读了每个文件,并将它们转换为浮点数并将它们附加到列表中.现在在 Python 2 中,调用 float(-3.65) 会返回 -3.65.然而,在 Python 3 中,float(-3.65) 返回-3.6499999999999999`,这会失去其精度.

Assume I have a text file with the numbers -3,65, 9,17, 1. I read each one, and once I convert them to float and append them to a list. Now in Python 2, calling float(-3.65) returns -3.65. In Python 3 however, float(-3.65) returns-3.6499999999999999` which loses its precision.

我想打印浮点数列表,[-3.6499999999999999, 9.1699999999999999, 1.0] 仅包含 2 个小数点.按照 '%.1f' % round(n, 1) 的行做一些事情会返回一个字符串.如何返回浮点数的所有两个小数点的列表,而不是字符串?到目前为止,我使用 [round(num, 2) for num in list] 对其进行了四舍五入,但需要设置小数点/精度而不是 round().

I want to print the list of floats, [-3.6499999999999999, 9.1699999999999999, 1.0] with 2 decimal points only. Doing something along the lines of '%.1f' % round(n, 1) would return a string. How can I return a list of all two decimal points of floats, and not strings? So far, I rounded it using [round(num, 2) for num in list] but would need to set the decimal points / precision instead of round().

推荐答案

一句话,你不能.

3.65 不能完全表示为 float.您得到的数字是与 3.65 最接近的数字,具有精确的 float 表示.

3.65 cannot be represented exactly as a float. The number that you're getting is the nearest number to 3.65 that has an exact float representation.

(旧的?)Python 2 和 3 之间的区别纯粹是由于默认格式.

The difference between (older?) Python 2 and 3 is purely due to the default formatting.

我在 Python 2.7.3 和 3.3.0 中都看到了以下内容:

I am seeing the following both in Python 2.7.3 and 3.3.0:

In [1]: 3.65
Out[1]: 3.65

In [2]: '%.20f' % 3.65
Out[2]: '3.64999999999999991118'

有关精确的十进制数据类型,请参阅 decimal.Decimal.

For an exact decimal datatype, see decimal.Decimal.

这篇关于Python 3 浮点小数点/精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)