function
<cmath> <ctgmath>

ldexp

double ldexp (double x, int exp);
     double ldexp  (double x     , int exp);
      float ldexpf (float x      , int exp);
long double ldexpl (long double x, int exp);
     double ldexp (double x     , int exp);
      float ldexp (float x      , int exp);
long double ldexp (long double x, int exp);
     double ldexp (double x     , int exp);
      float ldexp (float x      , int exp);
long double ldexp (long double x, int exp);
     double ldexp (T x          , int exp); // additional overloads for integral types
Generate value from significand and exponent
Returns the result of multiplying x (the significand) by 2 raised to the power of exp (the exponent).

lexpr(x,exp) = x * 2 exp
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).

Parameters

x
Floating point value representing the significand.
exp
Value of the exponent.

Return Value

The function returns:

x * 2 exp

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 occurs:

If an overflow range error occurs, the global variable errno is set to ERANGE.
If an overflow 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: FE_OVERFLOW is raised.

Example

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

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

  param = 0.95;
  n = 4;
  result = ldexp (param , n);
  printf ("%f * 2^%d = %f\n", param, n, result);
  return 0;
}


Output:

0.950000 * 2^4 = 15.200000

See also