scalbn, scalbln

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
scalbnscalbln
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       scalbnf( float arg, int exp );
(since C99)
double      scalbn( double arg, int exp );
(since C99)
long double scalbnl( long double arg, int exp );
(since C99)
float       scalblnf( float arg, long exp );
(since C99)
double      scalbln( double arg, long exp );
(since C99)
long double scalblnl( long double arg, long exp );
(since C99)

Multiplies a floating point value arg by FLT_RADIX raised to power exp. On binary systems it is equivalent to ldexp().

Contents

[edit] Parameters

arg - floating point value
exp - integer value

[edit] Return value

Returns arg×FLT_RADIXexp.

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("FLT_RADIX = %d\n", FLT_RADIX);
    printf("\n");
 
    printf("scalbn(1.0,+1023)   = %g\n",    scalbn(1.0,+1023));     /* 1 * 2^(+1023)  */
    printf("scalbn(1.0,-1074)   = %g\n",    scalbn(1.0,-1074));     /* 1 * 2^(-1074)  */
    printf("scalbn(5.0,0)       = %+.1f\n", scalbn(5.0,0));         /* 5 * 2^0        */
    printf("scalbn(+0.0,1)      = %+.1f\n", scalbn(+0.0,1));        /* 0 * 2^1        */
    printf("scalbn(-0.0,1)      = %+.1f\n", scalbn(-0.0,1));
    printf("scalbn(+INFINITY,1) = %+f\n",   scalbn(+INFINITY,1));   /* INFINITY * 2^1 */
    printf("scalbn(-INFINITY,1) = %+f\n",   scalbn(-INFINITY,1));
    printf("\n");
 
    /* "inexact" and "overflow" floating-point exceptions */
    /* Overflow does not set errno.                       */
    /* 1 * 2^(+1024)                                      */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("scalbn(1.0,1024)    = %f\n",   scalbn(1.0,1024));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* "inexact" and "underflow" floating-point exceptions */
    /* Underflow does not set errno.                       */
    /* 1 * 2^(-1075)                                       */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("scalbn(1.0,-1075)   = %g\n",   scalbn(1.0,-1075));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

FLT_RADIX = 2
 
scalbn(1.0,1023)    = 8.98847e+307
scalbn(1.0,-1074)   = 4.94066e-324
scalbn(5.0,0)       = +5.0
scalbn(+0.0,1)      = +0.0
scalbn(-0.0,1)      = -0.0
scalbn(+INFINITY,1) = +inf
scalbn(-INFINITY,1) = -inf
 
scalbn(1.0,1024)    = inf
Success
current exceptions raised:  FE_INEXACT FE_OVERFLOW
 
scalbn(1.0,-1075)   = 0
Success
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)
multiplies a number by 2 raised to a power
(function)
C++ documentation for scalbn