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

maxTemp = [73, 71.5, 74.1, 72.3, 75.3]
sum     = math.fsum(maxTemp)
avgTemp = sum/len(maxTemp)

print("Average maximum temperature:")
print(avgTemp)

Output:

Average maximum temperature:

73.24

 


Copyright 2024 © pythontic.com