In C++, std::log1p(x) computes log(1 + x) in one step. The name means "log of 1 plus". It gives the same answer as std::log(1.0 + x) for normal values, but it stays accurate when x is very small.
The problem with log(1 + x) for a tiny x is that 1 + x is rounded and loses digits of x before the log is taken. log1p() never adds the 1 as a rounded number, so no digits are lost. It is the opposite of std::expm1().
std::log1p() is declared in <cmath> and is part of the group Exponential and logarithm. Exponentials and logarithms undo each other. A logarithm of zero is -inf, and a logarithm of a negative number is NaN.
Syntax
Include <cmath> and call std::log1p. Start with these lines.
C++
double a = std::log1p(1.0); // double version
float b = std::log1p(1.0f); // float version
Call
What it does
std::log1p(1.0)
Passes a double and returns a double. Here the result is 0.693147.
std::log1p(1.0f)
Passes a float, so the float overload runs and returns a float.
std::log1p(9)
An integer is converted to double. Returns 2.30259.
Write std::log1p so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float log1p(float x);
double log1p(double x);
long double log1p(long double x);
double log1p(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. A number greater than -1. It is most useful when it is close to zero. An integer is converted to double.
Return Value
Return Value
Description
log(1 + x)
The natural logarithm of 1 + x.
-inf
Returned when x is exactly -1.
NaN
Returned when x is less than -1.
C++ Examples
Example 1 — Common Values of log1p()
Print log1p() for a few numbers and see how it matches log(1 + x).
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {0.0, 1.0, -0.5, 9.0};
for (double v : values) {
std::cout << "log1p(" << v << ") = " << std::log1p(v) << '\n';
}
return 0;
}
Adding 1e-15 to 1.0 must be rounded to the nearest double, which is about 1.0000000000000011. The small number is already a bit wrong.
The log of that rounded number is 1.11022e-15, which is about ten percent too large.
log1p(x) skips the rounded addition and prints the right answer, 1e-15.
Example 3 — Price Changes as Log Returns
Finance often uses log returns, log1p(change). Log returns add up, so a rise of 10 percent and a fall of 10 percent can be combined by simple addition.
C++
#include <cmath>
#include <iostream>
int main() {
double up = std::log1p(0.10);
double down = std::log1p(-0.10);
double total = up + down;
std::cout << "Return +10%: " << up << '\n';
std::cout << "Return -10%: " << down << '\n';
std::cout << "Total: " << total << '\n';
std::cout << "Price after: " << 100.0 * std::exp(total) << '\n';
return 0;
}
log1p(0.10) is the log return of a 10% rise: 0.0953102. A fall of 10% is log1p(-0.10), which is -0.105361.
The two returns are added to give -0.0100503. The total is negative because a rise of 10% followed by a fall of 10% leaves you slightly down.
std::exp(total) turns the sum back into a factor. 100 * 0.99 is 99, which matches 100 * 1.1 * 0.9.
Frequently Asked Questions
std::log1p(x) returns the natural logarithm of 1 plus x. It is the accurate way to write log(1 + x) when x is very close to zero.
The value log(1 + x) as float, double, or long double. log1p(-1) is -inf, and x below -1 gives NaN.
Use log1p() when x is very small, for example below 0.0001. Then 1 + x loses digits to rounding. For large x there is no difference.
It is std::expm1(). expm1(log1p(x)) gives back x, and both are accurate for small values.
Include <cmath> and write std::log1p(). That always works and picks the C++ overloads. The global name ::log1p often works too, but the standard does not promise it, and it may only take a double.