Reference · Math
math.prod
math.prod(list | null, num=) -> numWhat it does
The product of a list of numbers, as Python's math.prod, times a start value that is
1 by default. The result is an int while the list and the start are all ints, and a
float as soon as one of them is a float. An empty list gives the start value.
Examples
print(math.prod([2, 3, 4]), math.prod([2, 3.5]), math.prod([0.5, 4]))print(math.prod([2, 3], 10), type_of(math.prod([2, 3])))24 7.0 2.060 intEdge cases
- An empty list, or
null, gives the start:1without one, else what was given, somath.prod([], 2.5)is2.5, a float. - A float start makes the result a float:
math.prod([2, 3], 1.0)is6.0. - Ints multiply exactly, and a product past what an int holds (about 9.2 × 10¹⁸) is a
runtime error,
integer overflow, not a float; Python's int would keep growing. A float product that overflows isinf, silently. math.prod(range(1, n + 1))ismath.factorial(n).
print(math.prod([]), math.prod(null), math.prod([], 2.5), math.prod([2, 3], 1.0))print(math.prod([1e308, 10]), math.prod(range(1, 6)) == math.factorial(5))1 1 2.5 6.0inf trueprint(math.prod([9223372036854775807, 2]))line 1: integer overflowErrors
A list item that is not a number is a runtime error, not an error value; so is an
argument that is not a list or null.
print(math.prod([2, "x"]))line 1: `math.prod` expects numbers, got strprint(math.prod(3))line 1: `math.prod` expects a list as argument 1, got intChaining
A list in a pipeline is the first argument, so xs | math.prod multiplies it out:
print([1, 2, 3, 4] | math.prod)print([1.1, 1.05, 0.9] | math.prod | round(3))241.04See also
sum, math.fsum, math.sumprod, vec.cumprod, math.factorial
Thread 0 posts
On math.prod(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo