English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ ldexp() function usage and example

C++ Library Function <cmath>

C ++The ldexp(x, exp) function in the product uses two parameters: x and exp, and returns x and2product, whose product is the power of exp, that is, x * 2 exp.

The function is in<cmath>defined in the header file.

Mathematically

ldexp(x, exp) = x * 2exp

ldexp() prototype [from C ++ 11Standard starts]

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 integer

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

ldexp() parameter

  • x -representing a floating-point value with significant digits.

  • exp -exponent value.

ldexp() return value

ldexp() function returns the expression x * 2 expvalue.

Example1:ldexp() function in C ++How does it work 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 running the program, the output is:

ldexp(x, exp) = 52.224

Example2:An integer type ldexp() function

#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 running the program, the output is:

ldexp(x, exp) = 800

  C++ Library Function <cmath>