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);
Errors and exceptions associated with this function:
- It is mandatory to give both the arguments otherwise it will give error no matching function for call to ‘ldexp()’.
- If we pass string as argument we will get error no matching function for call to ‘ldexp(const char [n], const char [n]) .
- If we pass std::numeric_limits::max() as both the arguments we will get inf(infinity) as output.
Time Complexity: O(1)
Auxiliary Space: O(1)
Examples:
Input : ldexp(5.35, 4) Output : 85.6 Input : ldexp(25, 5) Output : 800
# CODE 1
C++
// C++ implementation 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; } |
Output:
85.6
# CODE 2
C++
// 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; } |
Output:
800
Contact Us