C++ std::expm1
In C++, std::expm1 computes e raised to x minus 1. It preserves precision near zero, where subtracting 1 from a rounded exponential can lose significant digits.
A simple example
std::expm1
- Start with 0
- Do this Evaluate e⁰ minus 1
- Result 0
#include <iostream>
#include <cmath>
int main() {
std::cout << (std::expm1(0.0)) << '\n';
}
0
Syntax
std::expm1(rate)
Run these examples in C++
Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.
c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lessonRecover a small percentage increase
A dashboard measures a percentage change so small that rounding can hide it. Use a numerical operation designed for a change near zero to retain the information in the original reading.
#include <cmath>
#include <cstdio>
int main() {
const double rate = 0.01;
std::printf("%.3f\n", std::expm1(rate));
return 0;
}
0.010
A continuous rate of 0.01 gives a fractional increase of about 0.01005017.
Recover the net sensor change
A sensor has a continuous change of minus 0.03 over one run. The report needs the fractional change relative to the start.
Using -0.03 gives -0.0296 for this measurement.
Report a small gain
An amplifier model gives a log gain of 0.002. A technician converts it to the fractional gain above baseline.
Using 0.002 gives 0.0020 for this measurement.
What to watch for
The result is a fraction. Multiply by 100 before labelling it as a percentage.