C++ std::expm1() Function

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

Definition

In C++, std::expm1(x) gives ex - 1. The name means "exp minus 1". It does the same job as std::exp(x) - 1, but it keeps the correct digits when x is very small.

The reason is that exp(x) for a tiny x is just a little more than 1. Subtracting 1 then wipes out most of the digits. expm1() avoids that subtraction, so it is the right tool for small growth rates and interest rates.

std::expm1() 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::expm1. Start with these lines.

C++
double a = std::expm1(0.5);    // double version
float  b = std::expm1(0.5f);   // float version
CallWhat it does
std::expm1(0.5)Passes a double and returns a double. Here the result is 0.648721.
std::expm1(0.5f)Passes a float, so the float overload runs and returns a float.
std::expm1(1)An integer is converted to double. Returns 1.71828.

Write std::expm1 so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       expm1(float x);
double      expm1(double x);
long double expm1(long double x);
double      expm1(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. The exponent. It is most useful when it is close to zero. An integer is converted to double.

Return Value

Return ValueDescription
ex - 1The result, which is greater than -1 for any x.
-1For a very negative x the result gets close to -1.
infThe result is too big for the type, as with exp().

C++ Examples

Example 1 — Common Values of expm1()

Print expm1() for a few exponents and compare with what you expect from ex - 1.

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

int main() {
    double values[] = {0.0, 1.0, -1.0, 2.0};

    for (double v : values) {
        std::cout << "expm1(" << v << ") = " << std::expm1(v) << '\n';
    }
    return 0;
}

How It Works

  1. The loop gives each exponent to std::expm1(), which returns ex - 1.
  2. expm1(0) is 0, because e0 is 1 and 1 - 1 is zero.
  3. expm1(1) is e - 1, or 1.71828. A negative exponent stays above -1, so expm1(-1) is -0.632121.

Example 2 — Accuracy for a Tiny Number

For x = 1e-10, the true answer of ex - 1 is very close to 1e-10. Compare the two ways to compute it.

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

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

    std::cout << std::setprecision(10);
    std::cout << "exp(x) - 1 = " << std::exp(x) - 1.0 << '\n';
    std::cout << "expm1(x)   = " << std::expm1(x) << '\n';
    return 0;
}

How It Works

  1. std::setprecision(10) shows ten significant digits so the difference between the two results is visible.
  2. exp(x) is stored as 1.0000000001 with only about 16 digits. After taking away 1, just a few correct digits are left, so the output is 1.000000083e-10.
  3. expm1(x) never makes that subtraction, so it keeps the right answer 1e-10. The first number is wrong in the 8th digit.

Example 3 — Effective Rate from a Continuous Rate

A bank states a continuous rate. The rate you actually earn in one year is erate - 1, which is exactly expm1(rate).

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

int main() {
    double rates[] = {0.05, 0.001};

    for (double r : rates) {
        std::cout << "Rate " << r * 100 << "%: you earn "
                  << std::expm1(r) * 100 << "% in a year\n";
    }
    return 0;
}

How It Works

  1. The two rates are 5% and 0.1%. Multiplying by 100 turns each rate into a percent for printing.
  2. expm1(0.05) is 0.0512711, so a continuous rate of 5% really pays 5.12711% in one year.
  3. For the tiny rate 0.001, the answer is just a little above the rate: 0.10005%. This is where expm1() is most accurate.

Frequently Asked Questions

std::expm1(x) returns e raised to x, minus 1. It is the accurate way to write exp(x) - 1 when x is very close to zero.
The value exp(x) - 1 as float, double, or long double. For tiny x it keeps all its digits, while exp(x) - 1 loses them.
Use expm1() when x is close to zero, for example smaller than 0.01. Then exp(x) - 1 loses digits to rounding, but expm1() does not. For big x there is no difference.
The opposite is std::log1p(). log1p(expm1(x)) gives back x, and both are accurate for small values.
Include <cmath> and write std::expm1(). That always works and picks the C++ overloads. The global name ::expm1 often works too, but the standard does not promise it, and it may only take a double.

Next: fabs()

Keep going through the <cmath> functions. Absolute value of a floating-point number.

fabs() 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.

15 people found this page helpful