expm1(), expm1f(), expm1l() were introduced with C99, and are available under the <math.h> header file. These are used to calculate the Euler’s Number, e (2.7182818) raised to a power equal to the provided parameter minus 1.0, i.e ex – 1.
expm1(x), expm1f(x), exmp1l(x) = ex – 1
Example:
double expm1(double arg);
float expm1f(float arg);
long double expm1l(long double arg);
Syntax:
expm1(x);
exmp1f(x);
exmp1l(x);
Parameters:
| Function | Parameter |
|---|
| expm1(x) | x ⇒ float |
| expm1f(x) | x ⇒ double |
| expm1l(x) | x ⇒ long double |
Return Values:
1. If no error occurs:
| Function | Return Value |
|---|
| expm1(x) | ex – 1 ⇒ float |
| expm1f(x) | ex-1 ⇒ double |
| expm1l(x) | ex – 1 ⇒ long double |
2. If range error due to overflow occurs:
| Function | Return Value |
|---|
| expm1(x) | +HUGE_VAL |
| expm1f(x) | +HUGE_VALF |
| expm1l(x) | +HUGE_VALL |
3. If range error due to underflow occurs: If a range error occurs due to underflow, the rounded result is returned.
4. Exceptional cases: The errors reported are handled as specified in math_errhandling.
| Parameter | Return Value |
|---|
| ±0 | ±0 |
| NaN | NaN |
| -∞ | -1 |
| +∞ | +∞ |
Time Complexity: O(1)
Space Complexity: O(1)
Example 1: Below is the C program to implement exp1():
C
#include <math.h>
#include <stdio.h>
int main()
{
double arg = 2.2310233;
printf("%lf\n",
expm1(arg));
return 0;
}
|
Example 2: Below is the C program to implement exp1f():
C
#include <math.h>
#include <stdio.h>
int main()
{
float arg = 4.121;
printf("%f\n",
expm1f(arg));
return 0;
}
|
Example 3: Below is the C program to implement exp1l():
C
#include <math.h>
#include <stdio.h>
int main()
{
long double arg = 5.212323323441;
printf("%Lf\n",
expm1l(arg));
return 0;
}
|
Why use expm1, expm1f, expm1l?
- These functions are more accurate than the expression ex – 1if x → 0.
- They are very useful for financial calculations, like calculating (small) interest rates, and for calculating the inverse of hyperbolic functions.