In this C++ tutorial, you will learn how to find the value of e raised to power of given number minus 1 using expm1() function of cmath, with syntax and examples.
C++ expm1()
C++ expm1() returns the value of e raised to power of given argument minus 1.
Syntax
The syntax of C++ expm1() is
expm1(x)
where
Parameter | Description |
---|---|
x | A double, float, long double, or any integral type value. |
Returns
The return value depends on the type of value passed for parameter x.
The return value of expm1(x) is
- double if x is double or integral type.
- float if x is float.
- long double if x is long double.
The synopsis of expm1() function is
double expm1(double x);
float expm1(float x);
long double expm1(long double x);
double expm1(T x); // for integral type argument values
expm1() is a function of cmath library. Include cmath library in the program, if using expm1() function.
Example
In this example, we read a value from user into variable x, and find the product of exponent raised to number x minus 1, using expm1() function.
C++ Program
#include <iostream>
#include<cmath>
using namespace std;
int main() {
double x;
cout << "Enter a number : ";
cin >> x;
double result = expm1(x);
cout << "expm1(" << x << ") : " << result << endl;
}
Output
Enter a number : 2
expm1(2) : 6.38906
Program ended with exit code: 0
Enter a number : 0
expm1(0) : 0
Program ended with exit code: 0
Enter a number : -5
expm1(-5) : -0.993262
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned the syntax of C++ expm1(), and how to use this function to find (e^x – 1), with the help of examples.