Reference · Math

math.prod

signature
math.prod(list | null, num=) -> num

What 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])))
output
24 7.0 2.060 int

Edge cases

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))
output
1 1 2.5 6.0inf true
this one fails
print(math.prod([9223372036854775807, 2]))
output
line 1: integer overflow

Errors

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.

this one fails
print(math.prod([2, "x"]))
output
line 1: `math.prod` expects numbers, got str
this one fails
print(math.prod(3))
output
line 1: `math.prod` expects a list as argument 1, got int

Chaining

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))
output
241.04

See also

sum, math.fsum, math.sumprod, vec.cumprod, math.factorial

Thread
12Useful? Like it, or add to its thread below.

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