Overview
- The function fsum() returns the accurate sum of multiple floating point numbers in an iterable.
Example
# Example Python program that sums up the daily maximum temperature
# and finds the average maximum temperature using math.fsum()
import math
# A list containing maximum temp for 5 days
maxTemp = [73, 71.5, 74.1, 72.3, 75.3]
# Sum-up the values to find the mean temp
sum = math.fsum(maxTemp)
avgTemp = sum/len(maxTemp)
print("Sum of 5-day maximum temperature:")
print(sum)
print("Average maximum temperature:")
print(avgTemp)Output
Sum of 5-day maximum temperature:
366.2
Average maximum temperature:
73.24