The fsum() function in python math module

The fsum() function from the Python math module returns the accurate sum of the floating point numbers given in a Python iterable.

Math Updated 21 Aug 2021 1 min read

Overview

  • The function fsum() returns the accurate sum of multiple floating point numbers in an iterable.

Example

python
# 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

Output
Sum of 5-day maximum temperature:

            366.2

            Average maximum temperature:

            73.24