Python fsum function

Python fsum() function is similar to sum() function, returns the sum. However, fsum() returns the value keeping higher floating accuracy. fsum() takes an iterable as its argument. It's a method of math module.

>>> import math
>>> math.fsum([2,3,4])
9.0
>>> math.fsum(range(1,4))
6.0
>>> math.fsum([0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01])
0.1
>>> sum([0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01])
0.0999999999999999999 #sum is less accurate than math.fsum
















endmemo.com © 2024  | Terms of Use | Privacy | Home