English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and example of scalbn() function

C++ Library Function <cmath>

C ++The scalbn() function in takes two parameters: x and n, which calculates the product of x and FLT_RADX multiplied by n.

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

FLT_RADIX is the value of the base (integer base) in the exponential form of the exponent.

The function is in<cmath>Defined in the header file. In addition, you need to use the <cfloat> header file to use FLT_RADIX.

scalbn(x, n) = x * FLT_RADIXn

scalbn() prototype [from C ++ 11Standard library begins]

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 integer type

It is similar toscalbln() functionare the same, but it takes int as the second parameter.

scalbn() parameters

scalbn() accepts two parameters:

  • x -The value representing the number of significant digits.

  • n-The exponent value of FLT_RADIX.

The return value of scalbn()

The scalbn() function returns. x * FLT_RADIXn

If the result is too large to be represented by the return type's value, the function returns HUGE_VAL with the correct sign.

Example: How does scalbn() work?

#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 the program is run, the output is:

3.056 * 2^13 = 25034.8

  C++ Library Function <cmath>