ldexp Function

Calculates the result of a double precision floating-point number multiplied by an exponent of 2.

Include

<math.h>

Prototype

double ldexp(double x, int ex);

Arguments

x
floating-point value
ex
integer exponent

Return Value

Returns x * 2^ex. On an overflow, ldexp returns inf and on an underflow, ldexp returns 0.

Remarks

A range error will occur on overflow or underflow.

Example

#include <math.h>
#include <stdio.h>
#include <errno.h>

int main(void)
{
  double x,y;
  int n;

  errno = 0;
  x = -0.625;
  n = 2;
  y = ldexp (x, n);
  if (errno)
    perror("Error");
  printf("For a number = %f and an exponent = %d\n",
         x, n);
  printf("  ldexp(%f, %d) = %f\n\n",
         x, n, y);

  errno = 0;
  x = 2.5;
  n = 3;
  y = ldexp (x, n);
  if (errno)
    perror("Error");
  printf("For a number = %f and an exponent = %d\n",
         x, n);
  printf("  ldexp(%f, %d) = %f\n\n",
         x, n, y);

  errno = 0;
  x = 15.0;
  n = 10000;
  y = ldexp (x, n);
  if (errno)
    perror("Error");
  printf("For a number = %f and an exponent = %d\n",
         x, n);
  printf("  ldexp(%f, %d) = %f\n\n",
         x, n, y);
}

Example Output

For a number = -0.625000 and an exponent = 2
  ldexp(-0.625000, 2) = -2.500000

For a number = 2.500000 and an exponent = 3
  ldexp(2.500000, 3) = 20.000000

Error: range error
For a number = 15.000000 and an exponent = 10000
  ldexp(15.000000, 10000) = inf