Python math.fsum() Method

Python math 模块Python math Module

Python math.fsum(iterable)The method calculates the sum of elements in an iterable (tuple, array, list, etc.).

Python version: 2.6

Syntax

The syntax of the math.fsum() method is as follows:

math.fsum(iterable)

Parameter description:

  • iterable-- Required. An iterable object, which can be a list, tuple, or array. If the elements of the iterable are not numbers, a TypeError is returned.

Return Value

A floating-point value representing the sum of all elements in the iterable.

Example

The following example calculates the sum of all elements:

Example

# Import the math package
import math

# Calculate the sum of all elements
print(math.fsum([1, 2, 3, 4, 5]))
print(math.fsum([100, 400, 340, 500]))
print(math.fsum([1.7, 0.3, 1.5, 4.5]))

Output:

15.0
1340.0
8.0

Python math 模块Python math Module

Other Extensions