In this C++ tutorial, you will learn how to compute the value of x scaled by FLT_RADIX to the power exp. using scalbn() function of cmath, with syntax and examples.

C++ scalbn()

C++ scalbn(x, exp) computes the value of x scaled by FLT_RADIX to the power exp.

scalbn(x, exp) = x * FLT_RADIX^exp

Syntax

The syntax of C++ scalbn() is

</>
Copy
scalbn(x, exp)

where

ParameterDescription
xA double, float, long double, or integral type value.
expAn integer value.

Returns

The return value depends on the type of value passed for parameter x.

The return value of scalbn(x, exp) is

  • double if x is double or integral type value.
  • float if x is float.
  • long double if x is long double.

The synopsis of scalbn() function is

</>
Copy
double scalbn(double x, int exp);
float scalbn(float x, int exp);
long double scalbn(long double x, int exp);
double scalbn(Type1 x, int exp); // for integral types

scalbn() is a function of cmath library. Include cmath library at the start of program, if using scalbn() function.

Example

In this example, we read two values from user into variables x and exp, and compute result of scalbn(x, exp).

C++ Program

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

int main() {
    double x;
    int exp;
    cout << "Enter x   : ";
    cin >> x;
    cout << "Enter exp : ";
    cin >> exp;
    
    double result = scalbn(x, exp);
    cout << "scalbn(" << x << ") : " << result << endl;
}

Output

Enter x   : 5
Enter exp : 3
scalbn(5) : 40
Program ended with exit code: 0
Enter x   : 8
Enter exp : 1
scalbn(8) : 16
Program ended with exit code: 0

If x is NaN, scalbn() returns NaN.

Enter x   : nan
Enter exp : 3
scalbn(nan) : nan
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ scalbn(), and how to use this function, with the help of examples.