Compute the logarithm with a specified base (defaults to natural logarithm).
LOG([base,] expr)
## Overview Returns the logarithm of the value in the given base. LOG can be called with one argument (returns natural log, base e) or with two arguments (returns base-N log of the second argument). It is the most general of the log family and is convenient when the base is variable or parameterized. For fixed bases where a dedicated function exists, prefer LN, LOG2, or LOG10: those routines are implemented with base-specific identities that avoid the extra division by LN(base) and produce more accurate results for powers of the declared base. ## Behavior - Domain (value): (0, infinity). Zero or negative returns NULL. - Domain (base): (0, 1) union (1, infinity). Base <= 0 or equal to 1 returns NULL. - Range: all real numbers. - Returns NULL if any argument is NULL. - Single-argument form returns LN(expr). - Is strictly monotonic in the value for a fixed base (increasing when base > 1, decreasing when base < 1). ## Numeric precision - Computed as LN(expr) / LN(base) internally. This introduces a small extra rounding step compared with LN, LOG2, or LOG10. - For bases that are not exactly representable (for example, LOG(7, 49)), the result may be slightly off the mathematical integer; expect values such as 2.0000000000000004 rather than exactly 2. - Precision degrades when expr is close to 1 because LN(expr) is small. ## Compatibility - Conforms to the SQL standard notion of a parameterized logarithm. Single-argument LOG maps to natural logarithm. - Matches typical mathematical library implementations.
| Name | Type | Description |
|---|---|---|
base | Specifies the logarithm base. Must be a positive number other than 1. When omitted, the function returns the natural logarithm (base e). | |
expr | Specifies the positive input whose logarithm is returned. Must be strictly greater than 0; zero and negative values yield NULL. |
-- Single argument: natural logarithm
SELECT LOG(E());
-- Result: 1.0
-- Base-10 logarithm of 100
SELECT LOG(10, 100);
-- Result: 2.0
-- Base-2 logarithm of 8
SELECT LOG(2, 8);
-- Result: 3.0
-- Arbitrary base
SELECT LOG(3, 81);
-- Result: 4.0
-- Domain violation: non-positive value returns NULL
SELECT LOG(10, -5);
-- Result: NULL
-- Invalid base: 1 returns NULL (undefined)
SELECT LOG(1, 100);
-- Result: NULL