Compute ln(1 + x) with high precision for small x.
LOG1P(expr)
## Overview Returns ln(1 + x). LOG1P exists specifically to preserve precision when x is small. The straightforward expression LN(1 + x) accumulates rounding error at the 1 + x step for small x because 1 + x rounds to exactly 1 once x falls below 2^-52, destroying all the information you are trying to log. LOG1P avoids that collapse by computing the expansion without ever forming the explicit sum. LOG1P is indispensable in maximum-likelihood estimation, financial compounding over short periods, and numeric tests of identities like LN(EXP(x)) = x near zero. ## Behavior - Domain: (-1, infinity). Values less than or equal to -1 return NULL. - Range: all real numbers. - Returns NULL if the argument is NULL. - Is strictly increasing. - LOG1P(0) is exactly 0. - LOG1P(EXPM1(x)) equals x to within one ULP for all valid x. ## Numeric precision - Uses the IEEE 754 double-precision log1p routine, accurate to within one ULP. - Matches LN(1 + x) in the mathematical sense but preserves precision even when 1 + x would round to exactly 1 in DOUBLE arithmetic. - For |x| larger than roughly 0.5, the precision benefit is negligible; LN(1 + x) and LOG1P(x) agree to within one ULP. ## Compatibility - Conforms to the C99 and IEEE 754 log1p conventions. - Matches typical mathematical library implementations.
| Name | Type | Description |
|---|---|---|
expr | Specifies the real-valued input. Must be strictly greater than -1. Values less than or equal to -1 return NULL. |
-- Basic literal
SELECT LOG1P(0);
-- Result: 0.0
-- LOG1P of 1 equals LN(2)
SELECT LOG1P(1);
-- Result: 0.6931471805599453
-- Precision advantage for small values
SELECT LOG1P(1E-10);
-- Result: 9.999999999500001E-11
-- Compare with naive LN(1 + x)
SELECT LOG1P(1E-15), LN(1 + 1E-15);
-- Result: 1.0E-15, 1.1102230246251565E-15
-- Domain violation: x <= -1 returns NULL
SELECT LOG1P(-1), LOG1P(-2);
-- Result: NULL, NULL
-- Inverse relationship with EXPM1
SELECT LOG1P(EXPM1(2.0));
-- Result: 2.0