How to get an accurate floating point sum of values from a list in Python

3 Answers

0 votes
import math

List = [-34, -12, 99, -51, 12]

print(math.fsum(List))


'''
run:

14.0

'''

 



answered Oct 14, 2017 by avibootz
0 votes
import math

List = [-34.3, -12.1, 99.9, -51.6, 12.5]

print(math.fsum(List))


'''
run:

14.400000000000007

'''

 



answered Oct 14, 2017 by avibootz
0 votes
import math

print(math.fsum([-34.3, -12.1, 99.9, -51.6, 12.9]))


'''
run:

14.800000000000008

'''

 



answered Oct 14, 2017 by avibootz
...