# ldexp() function in C/C++

The ldexp() is a built-in function in C/C++ gives the product of any variable x and 2 raised to the power of exp by taking two arguments x and exp i.e., x * 2^exp

Syntax:

```float ldexp (float x, int exp)
double ldexp (double x, int exp)
long double ldexp (long double x, int exp)
double ldexp (T x, int exp)
```

Parameter: The function accepts two mandatory parameter x and exp which specifies the two variables described in the definition.

Return Value: The function returns the value of the expression x * 2^exp. It returns a value of type long double, float or double.

Below programs illustrates the above function.

Program 1:

 `// C++ program to illustrate the ` `// ldexp() function ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``double` `x = 6, result; ` `    ``int` `exp` `= 2; ` ` `  `    ``// It returns x*(2^exp) ` `    ``result = ``ldexp``(x, ``exp``); ` ` `  `    ``// print the result ` `    ``cout << ``"ldexp(x, exp) = "` `<< result << endl; ` `    ``return` `0; ` `} `

Output:

```ldexp(x, exp) = 24
```

Program 2:

 `// C++ program to illustrate the ` `// ldexp() function ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``double` `result; ` `    ``int` `x = 20, ``exp` `= 9; ` `    ``// It returns x*(2^exp) ` `    ``result = ``ldexp``(x, ``exp``); ` ` `  `    ``// prints the result ` `    ``cout << ``"ldexp(x, exp) = "` `<< result << endl; ` `    ``return` `0; ` `} `

Output:

```ldexp(x, exp) = 10240
```

Errors and Exceptions: If the magnitude of the result is too large to be represented by a value of the return type, the function returns HUGE_VAL (or HUGE_VALF or HUGE_VALL) with the proper sign, and an overflow range error occurs.

Below programs illustrates the above error.

 `// C++ program to illustrate the ` `// ldexp() function ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``double` `result; ` `    ``int` `x = 20, ``exp` `= 10000; ` ` `  `    ``// It returns x*(2^exp) ` `    ``result = ``ldexp``(x, ``exp``); ` ` `  `    ``// prints the result ` `    ``cout << ``"ldexp(x, exp) = "` `<< result << endl; ` `    ``return` `0; ` `} `

Output:

```ldexp(x, exp) = inf
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.