C++ ldexp()

The ldexp() function in C++ takes two arguments: x and exp and returns the product of x and 2 raised to the power of exp i.e. x * 2exp.

The function is defined in <cmath> header file.

Mathematically,

ldexp(x, exp) = x * 2exp

ldexp() prototype [As of C++ 11 standard]

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

The ldexp() function takes two arguments and returns a value of type double, float or long double.


ldexp() Parameters

  • x - The floating point value representing the significand.
  • exp - The value of exponent.

ldexp() Return value

The ldexp() function returns the value of the expression x * 2exp.


Example 1: How ldexp() function works in C++?

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
	double x = 13.056, result;
	int exp = 2;
	result = ldexp(x , exp);
	cout << "ldexp(x, exp) = " << result << endl;

	return 0;
}

When you run the program, the output will be:

ldexp(x, exp) = 52.224

Example 2: ldexp() function with integral type

#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
	double result;
	int x = 25, exp = 5;

	result = ldexp(x , exp);
	cout << "ldexp(x, exp) = " << result << endl;

	return 0;
}

When you run the program, the output will be:

ldexp(x, exp) = 800
Did you find this article helpful?