C++ std::log1p() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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
CallWhat 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

ParameterDescription
xRequired. A number greater than -1. It is most useful when it is close to zero. An integer is converted to double.

Return Value

Return ValueDescription
log(1 + x)The natural logarithm of 1 + x.
-infReturned when x is exactly -1.
NaNReturned 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;
}

How It Works

  1. Each number goes to std::log1p(), which works out log(1 + x).
  2. log1p(0) is log(1), which is 0. log1p(1) is log(2), or 0.693147.
  3. log1p(-0.5) is log(0.5), so it is negative: -0.693147. log1p(9) is log(10), or 2.30259.

Example 2 — Accuracy for a Tiny Number

For x = 1e-15, the answer of log(1 + x) should be almost exactly 1e-15. See which function gets it right.

C++
#include <cmath>
#include <iostream>

int main() {
    double x = 1e-15;

    std::cout << "log(1 + x) = " << std::log(1.0 + x) << '\n';
    std::cout << "log1p(x)   = " << std::log1p(x) << '\n';
    return 0;
}

How It Works

  1. 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.
  2. The log of that rounded number is 1.11022e-15, which is about ten percent too large.
  3. 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;
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: log2()

Keep going through the <cmath> functions. Base-2 logarithm of a number.

log2() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful