ldexp() Function with Example in C++

C++ ldexp() function: Here, we are going to learn about the ldexp() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on May 18, 2020

C++ ldexp() function

ldexp() function is a library function of cmath header, it is used to calculate the value from significand and exponent, it returns the multiplication of significand and 2 raised to the power of the exponent. It accepts two values (significand and exponent) and returns the result of significand x 2^exponent.

Syntax of ldexp() function:

C++11:

     double ldexp (double significand, int exponent);
      float ldexp (float significand, int exponent);
long double ldexp (long double significand, int exponent);
     double ldexp (T significand, int exponent);

Parameter(s):

  • significand, exponent – represent the values to calculate the result from significand and exponent.

Return value:

It returns the multiplication of significand and 2 raised to the power of the exponent.

Example:

    Input:
    float s = 10.0f;
    float y = 2.0f
    
    Function call:
    ldexp(x, y);
    
    Output:
    40 (10.0 * 2.0^2.0 = 10.0 * 4.0 = 40)

C++ code to demonstrate the example of ldexp() function

// C++ code to demonstrate the example of
// ldexp() function

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float s = 10.0f;
    float e = 2.0f;

    cout << "ldexp(" << s << "," << e << ") = " << ldexp(s, e) << endl;

    s = 20.0f;
    e = 0.0f;
    cout << "ldexp(" << s << "," << e << ") = " << ldexp(s, e) << endl;

    s = 36.2f;
    e = 0.49f;
    cout << "ldexp(" << s << "," << e << ") = " << ldexp(s, e) << endl;

    return 0;
}

Output

ldexp(10,2) = 40
ldexp(20,0) = 20
ldexp(36.2,0.49) = 36.2

Reference: C++ ldexp() function



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.