简体   繁体   English

如何检查浮点数列表加起来是否为整数?

[英]How to check if a list of floats adds up to a whole number?

I would like to find the sum of a list of floats and check if it is a whole number or not:我想找到浮点数列表的总和并检查它是否是整数:

l = [0.85, 0.85, 0.15, 0.15]

The sum of l is 2.0, obviously.显然, l的总和是 2.0。 But Python does not agree, due to floating point limitations :但 Python 不同意,由于浮点数限制

> sum(l)
1.9999999999999998

Therefore my method of choice, sum(l).is_integer() , will return False .因此,我选择的方法sum(l).is_integer()将返回False

What would be a better method to evaluate if lists sum to a whole number?评估列表是否为整数的更好方法是什么?

You can use the decimal package.您可以使用decimal包。

>>> from decimal import Decimal
>>> l = [Decimal(x) for x in ['0.85', '0.85', '0.15', '0.15']]
>>> s = sum(l)
>>> s
Decimal('2.00')
>>> s == int(s)
True

You can use a combination of math.isclose and the inbuilt round function.您可以结合使用math.isclose和内置的round函数。

>>> from math import isclose  
>>> l = [0.85, 0.85, 0.15, 0.15]
>>> s = sum(l)
>>> isclose(s, round(s))
True

When you need exact arithmetic, the best solution is to work with integers only.当您需要精确算术时,最好的解决方案是仅使用整数。 In this case you could round each of your inputs to 2 or 3 decimal places for the check.在这种情况下,您可以将每个输入四舍五入到 2 或 3 个小数位以进行检查。

l = [0.85, 0.85, 0.15, 0.15]
number_of_places = 3
multiplier = 10 ** number_of_places
>>> sum(int(round(x*multiplier)) for x in l)
2000
>>> sum(int(round(x*multiplier)) for x in l) % multiplier == 0
True

Apart Decimal, you can also use the fractions module, which will enable handling arbitrary denominators like:除了小数,您还可以使用分数模块,它可以处理任意分母,例如:

from fractions import Fraction
print(Fraction(1,3) + Fraction(4,7) + Fraction(2,21))

Also works with Fraction('0.85') + Fraction('0.15') or Fraction('2/6') .也适用于Fraction('0.85') + Fraction('0.15')Fraction('2/6')

Note that the result of adding Fractions is a Fraction, eventually with denominator == 1, so instead of testing is_integer() , you have to replace with a denominator check, or with equality of int conversion:请注意,添加 Fractions 的结果是一个 Fraction,最终分母 == 1,因此,您必须替换为分母检查,或 int 转换的相等性,而不是测试is_integer()

collection = [Fraction(each) for each in ['0.85', '0.85', '0.15', '0.15']]
sum = sum(collection)
print(sum.denominator == 1)
print(int(sum) == sum)

In some languages including Smalltalk, Fractions are automatically reduced to Integer when denominator is 1, making the code a bit less brittle...在包括 Smalltalk 在内的一些语言中,当分母为 1 时,分数会自动减少为整数,从而使代码不那么脆弱……

^((1/3) + (4/7) + (2/21)) isInteger

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在Python中大于x,则将浮点列表转换为最接近的整数 - Convert a list of floats to the nearest whole number if greater than x in Python 在忽略浮点数和字符串的情况下如何返回整数(如果有整数) - How to return int if whole number, while ignoring floats and strings 检查列表是否具有特定数量的整数 - Check if a list has specific number of whole numbers 获取加起来为数字“n”的数字列表(使用数字重复)——如何考虑多次使用相同数字的情况? - Get a list of numbers that adds up to a number "n" (using repetition of number) - how would one consider the case of using same number multiple times? 如何检查复数是否为整数 - How to check if complex number is a whole number Python 和浮动 - 只打印整数 - Python and Floats - Print only the Whole Number 总结一个浮点数列表(Python) - Summing up a list of floats(Python) 如何检查浮点值是否为整数 - How to check if a float value is a whole number 如何检查列表中的所有元素是否都是整数 - How to check if all elements in a list are whole numbers 如何仅将整数(如 25.0)的浮点数更改为 integer 并将 25.9 等浮点数保留为 python 中的浮点数 - how to only change floats that are whole numbers like 25.0, into integer and keep floats like 25.9 as floats in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM