ldexp, ldexpf, ldexpl

Header: <math.h>

1-3) Multiplies a floating-point value arg by the number 2 raised to the exp power.

# Declarations

float ldexpf( float arg, int exp );

(since C99)

double ldexp( double arg, int exp );
long double ldexpl( long double arg, int exp );

(since C99)

#define ldexp( arg, exp )

(since C99)

# Parameters

# Return value

If no errors occur, arg multiplied by 2 to the power of exp (arg×2exp) is returned.

# Notes

On binary systems (where FLT_RADIX is 2), ldexp is equivalent to scalbn.

The function ldexp (“load exponent”), together with its dual, frexp, can be used to manipulate the representation of a floating-point number without direct bit manipulations.

On many implementations, ldexp is less efficient than multiplication or division by a power of two using arithmetic operators.

# Example

#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
 
int main(void)
{
    printf("ldexp(7, -4) = %f\n", ldexp(7, -4));
    printf("ldexp(1, -1074) = %g (minimum positive subnormal double)\n",
            ldexp(1, -1074));
    printf("ldexp(nextafter(1,0), 1024) = %g (largest finite double)\n",
            ldexp(nextafter(1,0), 1024));
 
    // special values
    printf("ldexp(-0, 10) = %f\n", ldexp(-0.0, 10));
    printf("ldexp(-Inf, -1) = %f\n", ldexp(-INFINITY, -1));
 
    // error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("ldexp(1, 1024) = %f\n", ldexp(1, 1024));
    if (errno == ERANGE)
        perror("    errno == ERANGE");
    if (fetestexcept(FE_OVERFLOW))
        puts("    FE_OVERFLOW raised");
}

# See also