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
Call
What 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
Parameter
Description
x
Required. The exponent. It is most useful when it is close to zero. An integer is converted to double.
Return Value
Return Value
Description
ex - 1
The result, which is greater than -1 for any x.
-1
For a very negative x the result gets close to -1.
inf
The 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;
}
std::setprecision(10) shows ten significant digits so the difference between the two results is visible.
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.
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;
}
Output
Rate 5%: you earn 5.12711% in a year
Rate 0.1%: you earn 0.10005% in a year
How It Works
The two rates are 5% and 0.1%. Multiplying by 100 turns each rate into a percent for printing.
expm1(0.05) is 0.0512711, so a continuous rate of 5% really pays 5.12711% in one year.
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.