Free cookie consent management tool by TermsFeed The fsum() function in python math module | Pythontic.com

The fsum() function in python math module

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

 


Copyright 2025 © pythontic.com