function
<cmath> <ctgmath>

scalbn

     double scalbn  (double x     , int n);      float scalbnf (float x      , int n);long double scalbnl (long double x, int n);
     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); // additional overloads for integral types
Scale significand using floating-point base exponent
Scales x by FLT_RADIX raised to the power of n, returning the same as:

scalbn(x,n) = x * FLT_RADIXn

Presumably, x and n are the components of a floating-point number in the system; In such a case, this function may be optimized to be more efficient than the theoretical operations to compute the value explicitly.

On most platforms, FLT_RADIX is 2, making this function equivalent to ldexp.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type).

There also exists another version of this function: scalbln, which is identical, except that it takes a long int as second argument.

Parameters

x
Value representing the significand.
exp
Value of the exponent.

Return Value

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 (or HUGE_VALF or HUGE_VALL) with the proper sign, and an overflow range error may occur (if too small, the function returns zero, and an underflow range error may occur).

If a range error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to ERANGE.
- And math_errhandling has MATH_ERREXCEPT set: either FE_OVERFLOW or FE_UNDERFLOW is raised.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* scalbn example */
#include <stdio.h>      /* printf */
#include <float.h>      /* FLT_RADIX */
#include <math.h>       /* scalbn */

int main ()
{
  double param, result;
  int n;

  param = 1.50;
  n = 4;
  result = scalbn (param , n);
  printf ("%f * %d^%d = %f\n", param, FLT_RADIX, n, result);
  return 0;
}

Output:

1.500000 * 2^4 = 24.000000


See also