Reference · Math

math.log

signature
math.log(num, num=) -> float

What it does

The natural logarithm of a number, as a float, as Python's math.log. With a second argument, the logarithm to that base: math.log(8, 2) is 3.0. The number must be above zero, and the base above zero and not 1.

Examples

print(math.log(1), math.log(math.e), math.log(10))print(math.log(8, 2), math.log(100, 10), math.log(81, 3))
output
0.0 1.0 2.3025850929940463.0 2.0 4.0

Edge cases

print(math.log(1000, 10), math.log10(1000))print(math.log(8, 0.5))print(math.log(0.5), math.log(5e-324))print(math.log(math.inf), math.log(math.nan), math.log(8, math.inf))
output
2.9999999999999996 3.0-3.0-0.6931471805599453 -744.4400719213812inf NaN 0.0

Errors

Zero or a negative number is a runtime error (a domain error), not an error value: it stops the request, and ?? does not catch it, so test the value first when it can be zero. A base of 1, 0 or less is a domain error too, named as the base. A value that is not a number is a runtime error; a decimal is converted first with float.

this one fails
print(math.log(0))
output
line 1: `math.log`: math domain error (0)
this one fails
print(math.log(8, 1))
output
line 1: `math.log`: math domain error (base 1)
this one fails
print(math.log(-1))
output
line 1: `math.log`: math domain error (-1)

Chaining

In a pipeline the number comes first, so x | math.log(2) is the logarithm of x to base 2. For a whole list, vec.log takes the natural logarithm of each item:

print(1024 | math.log(2))print([1, 10, 100] | map((x) => math.log(x, 10)), vec.log([1, math.e]))print(math.exp(2) | math.log)
output
10.0[0.0, 1.0, 2.0] [0.0, 1.0]2.0

See also

math.log2, math.log10, math.log1p, math.exp, vec.log

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

Thread 0 posts

On math.log(): 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