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