math.log

Shared

Returns the logarithm of a number in a given base


Syntax

local result = math.log(
    value,
    base = math.exp(1)
)

Parameters

TypeNameDescription
numbervaluePositive number to compute the logarithm of
numberbaseLogarithm base - defaults to e (natural log)

Returns

TypeNameDescription
numberresultLogarithm of value in the given base

Examples

Natural log of e
local result = math.log(math.exp(1))

engine.print("info", result) -- 1.0
Natural log of 1 is always 0
local result = math.log(1)

engine.print("info", result) -- 0.0
Log base 10
local result = math.log(1000, 10)

engine.print("info", result) -- 3.0
Log base 2
local result = math.log(8, 2)

engine.print("info", result) -- 3.0
Compute number of digits in an integer
local n = 12345
local digits = math.floor(math.log(n, 10)) + 1

engine.print("info", digits) -- 5
Compute how many doublings to reach a target
local start = 1
local target = 1024
local doublings = math.ceil(math.log(target / start, 2))

engine.print("info", doublings) -- 10
Verify log and exp are inverses
local value = 5.0
local result = math.log(math.exp(value))

engine.print("info", result) -- 5.0

On this page