expm1() Function
The expm1()
function in C calculates the value of the exponential function minus one. It is especially useful for achieving higher precision when computing e raised to a small power, thereby avoiding significant numerical errors that can occur with direct calculations.
Syntax of expm1()
double expm1(double x);
float expm1f(float x);
long double expm1l(long double x);
Parameters
Parameter | Description |
---|---|
x | The exponent value for which the function computes ex – 1. |
Return Value
The function returns the computed value of e raised to the power of x, minus one.
It is important to note that for small magnitude values, using expm1()
can provide a more precise result compared to computing exp(x) - 1
directly, as it minimizes floating-point rounding errors.
Examples for expm1()
Example 1: Calculating expm1() for a Small Exponent Value
This example demonstrates the use of expm1()
to compute the result for a small exponent, highlighting its accuracy.
Program
#include <stdio.h>
#include <math.h>
int main() {
double x = 0.001;
double result = expm1(x);
printf("expm1(%.3f) = %.10f\n", x, result);
return 0;
}
Explanation:
- The program defines a small exponent value
0.001
. - It computes e0.001 – 1 using
expm1()
, which is more accurate than usingexp(0.001) - 1
. - The computed result is printed with high precision to demonstrate the function’s accuracy for small values.
Program Output:
expm1(0.001) = 0.0010005002
Example 2: Computing expm1() for a Larger Exponent Value
This example illustrates the use of expm1()
for a larger exponent value, showing its behavior with higher magnitudes.
Program
#include <stdio.h>
#include <math.h>
int main() {
double x = 2.0;
double result = expm1(x);
printf("expm1(%.1f) = %.10f\n", x, result);
return 0;
}
Explanation:
- The program sets a larger exponent value of
2.0
. - It calculates e2.0 – 1 using
expm1()
to demonstrate the function’s capability with larger inputs. - The result is printed to display the computed value.
Program Output:
expm1(2.0) = 6.3890560989