列表中的浮点数总和为.00000001

时间:2019-06-30 15:21:52

标签: python floating-point sum

我正在编写一个python函数,以在sum(list)不等于1.0的情况下获取概率列表(每个均四舍五入到小数点后两位),并将列表的第一个元素更新为制作sum(list) = 1.0。功能如下:

def round_off_list_to_1(prob_list):
    if len(prob_list) != 0:
        prob_list[0] = round(prob_list[0] + (1.0 - sum(prob_list)), 2)
    assert sum(prob_list) == 1, "Sum of random probabilities not equal to 1.0"
    return prob_list

我不了解list = [.59, .33, .08]sum(list) = .9999999的含义。该函数被调用,并且列表保持不变,并且断言失败。

0 个答案:

没有答案