ldexp() in C++

The ldexp() function takes two arguments a and b and returns the product of a and 2 raised to the power of b i.e., a * (2b).

Syntax:

double ldexp (double a, double b);
float ldexp (float a, float b);
long double ldexp (long double a, long double b);

    Parameter:



  • The ldexp() function takes two arguments a, b and computes a * (2b).
  • Here a and b can be anything which is valid.
  • Return:

  • The ldexp() function returns a * (2^b) if we pass a and b as the arguments.

    Errors and exceptions associated with this function:

  1. It is mandatory to give both the arguments otherwise it will give error no matching function for call to ‘ldexp()’.
  2. If we pass string as argument we will get error no matching function for call to ‘ldexp(const char [n], const char [n]) .
  3. If we pass std::numeric_limits::max() as both the arguments we will get inf(infinity) as output.

Examples:

Input  : ldexp(5.35, 4)
Output : 85.6
Input  : ldexp(25, 5)
Output : 800

# CODE 1

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implemenatation of the
// above function
#include <cmath>
#include <iostream>
  
using namespace std;
  
int main()
{
    double a = 5.35;
    int b = 4;
    cout << ldexp(a, b);
    return 0;
}

chevron_right


Output:

85.6

# CODE 2

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implementation of the
// above function
#include <cmath>
#include <iostream>
using namespace std;
  
int main()
{
    int a = 25, b = 5;
    cout << ldexp(a, b);
    return 0;
}

chevron_right


Output:

800


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.