ldexp

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
ldexp
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       ldexpf( float arg, int exp );
(since C99)
double      ldexp( double arg, int exp );
long double ldexpl( long double arg, int exp );
(since C99)

Multiplies a floating point value arg by 2 raised to power exp.

Contents

[edit] Parameters

arg - floating point value
exp - integer value

[edit] Return value

Returns arg×2exp.

If the result is too large for the underlying type, range error occurs and HUGE_VAL is returned.

[edit] Example

#include <stdio.h>
#include <math.h>
#include <fenv.h>
#include <errno.h>
#include <float.h>
#include <string.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{
    printf("ldexp(1.0,+1023)   = %g\n",    ldexp(1.0,+1023));     /* 1 * 2^(+1023)  */
    printf("ldexp(1.0,-1074)   = %g\n",    ldexp(1.0,-1074));     /* 1 * 2^(-1074)  */
    printf("ldexp(5.0,0)       = %+.1f\n", ldexp(5.0,0));         /* 5 * 2^0        */
    printf("ldexp(+0.0,1)      = %+.1f\n", ldexp(+0.0,1));        /* 0 * 2^1        */
    printf("ldexp(-0.0,1)      = %+.1f\n", ldexp(-0.0,1));
    printf("ldexp(+INFINITY,1) = %+f\n",   ldexp(+INFINITY,1));   /* INFINITY * 2^1 */
    printf("ldexp(-INFINITY,1) = %+f\n",   ldexp(-INFINITY,1));
    printf("\n");
 
    /* "inexact" and "overflow" floating-point exceptions */
    /* Overflow does set errno (range error).             */
    /* 1 * 2^(+1024)                                      */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("ldexp(1.0,1024)    = %f\n",   ldexp(1.0,1024));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* "inexact" and "underflow" floating-point exceptions */
    /* Underflow does set errno (range error).             */
    /* 1 * 2^(-1075)                                       */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("ldexp(1.0,-1075)   = %g\n",   ldexp(1.0,-1075));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

ldexp(1.0,1023)    = 8.98847e+307
ldexp(1.0,-1074)   = 4.94066e-324
ldexp(5.0,0)       = +5.0
ldexp(+0.0,1)      = +0.0
ldexp(-0.0,1)      = -0.0
ldexp(+INFINITY,1) = +inf
ldexp(-INFINITY,1) = -inf
 
ldexp(1.0,1024)    = inf
Numerical result out of range
current exceptions raised:  FE_INEXACT FE_OVERFLOW
 
ldexp(1.0,-1075)   = 0
Numerical result out of range
current exceptions raised:  FE_INEXACT FE_UNDERFLOW

[edit] See also

(C99)(C99)
breaks a number into significand and a power of 2
(function)
(C99)(C99)(C99)(C99)(C99)(C99)
computes efficiently a number times FLT_RADIX raised to a power
(function)
C++ documentation for ldexp