C++ scalbn()

The scalbn() function in C++ takes two arguments: x and n, and scales x by FLT_RADIX raised to the power n.

In simple terms, the scalbn() function returns the product of x and FLT_RADIX raised to the power n.

FLT_RADIX is the value of the radix (integer base) of the exponent representation.

The function is defined in <cmath> header file. Also, you need to use <cfloat> header file to use FLT_RADIX.


scalbn(x, n) = x * FLT_RADIXn

scalbn() prototype [As of C++ 11 standard]

double scalbn (double x, int n);
float scalbn (float x, int n);
long double scalbn (long double x, int n);
double scalbn (T x, int n); // Here, T is an integral type

It is identical to scalbln() function except that it takes int as second parameter.


scalbn() Parameters

The scalbn() takes two arguments:

  • x - The value representing the significand.
  • n - The value of exponent of FLT_RADIX.

scalbn() Return Value

The scalbn() function returns x * FLT_RADIXn.

If the magnitude of the result is too large to be represented by a value of the return type, the function returns HUGE_VAL with the proper sign.


Example: How scalbn() works?

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

int main ()
{
	int n = 13;
	double x = 3.056, result;
	
	result = scalbn (x, n);
	cout << x << " * " << FLT_RADIX << "^" << n << " = " << result << endl;
	
	return 0;
}

When you run the program, the output will be:

3.056 * 2^13 = 25034.8
Did you find this article helpful?