Python math.fsum() – Sum of Values in Iterable

Python math.fsum()

math.fsum(x) function returns the sum of items in the iterable x.

Syntax

The syntax to call fsum() function is

math.fsum(x)

where

ParameterRequiredDescription
xYesAn iterable.

Examples

Sum of items in a list using math.fsum() function.

Python Program

import math

x = list([2.4, 5.1, 6.7])
result = math.fsum(x)
print('fsum(x) :', result)
Run

Output

fsum(x) : 14.2

Sum of items in a tuple using math.fsum() function.

Python Program

import math

x = tuple([2.4, 5.1, 6.7])
result = math.fsum(x)
print('fsum(x) :', result)
Run

Output

fsum(x) : 14.2

Sum of items in a range using math.fsum() function.

Python Program

import math

x = range(5)
result = math.fsum(x)
print('fsum(x) :', result)
Run

Output

fsum(x) : 10.0

Return value of fsum() when there is one or more infinity values, and the rest float/integral values, in the iterable.

Python Program

import math

x = [math.inf, 5, 3.2]
result = math.fsum(x)
print('fsum(x) :', result)
Run

Output

fsum(x) : inf

Return value of fsum() when there is at least one nan value in the iterable.

Python Program

import math

x = [math.nan, 5, 3.2]
result = math.fsum(x)
print('fsum(x) :', result)
Run

Output

fsum(x) : nan

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.fsum() function.