The fsum method of the math module

The fsum method of the math module returns the sum of all terms of a sequence of numbers. The return type is always fractional.

Syntax

pythonimport math

math.fsum(sequence of numbers)

Example

Let's find the sum of all the members of the sequence of numbers in the list:

pythonimport math

lst = [1, 2, 3]
print(math.fsum(lst))

Result of code execution:

python6.0

Example

Now let's apply the fsum method to a sequence of negative numbers:

pythonimport math

lst = [-2, -4, -6]
print(math.fsum(lst))

Result of code execution:

python-12.0

See also

  • function sum,
    which also returns the sum of the numbers
  • method factorial of module math,
    which returns the factorial of a number
  • function min,
    which returns the minimum number
  • function max,
    which returns the maximum number