scalbn, scalbnf, scalbnl, scalbln, scalblnf, scalblnl

Header: <math.h>

1-3,5-7) Multiplies a floating-point value arg by FLT_RADIX raised to power exp.

# Declarations

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)

#define scalbn( arg, 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)

#define scalbln( arg, exp )

(since C99)

# Parameters

# Return value

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

# Notes

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

Although scalbn and scalbln are specified to perform the operation efficiently, on many implementations they are less efficient than multiplication or division by a power of two using arithmetic operators.

The scalbln function is provided because the factor required to scale from the smallest positive floating-point value to the largest finite one may be greater than 32767, the standard-guaranteed INT_MAX. In particular, for the 80-bit long double, the factor is 32828.

# 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("scalbn(7, -4) = %f\n", scalbn(7, -4));
    printf("scalbn(1, -1074) = %g (minimum positive subnormal double)\n",
            scalbn(1, -1074));
    printf("scalbn(nextafter(1,0), 1024) = %g (largest finite double)\n",
            scalbn(nextafter(1,0), 1024));
 
    // special values
    printf("scalbn(-0, 10) = %f\n", scalbn(-0.0, 10));
    printf("scalbn(-Inf, -1) = %f\n", scalbn(-INFINITY, -1));
 
    // error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("scalbn(1, 1024) = %f\n", scalbn(1, 1024));
    if (errno == ERANGE)
        perror("    errno == ERANGE");
    if (fetestexcept(FE_OVERFLOW))
        puts("    FE_OVERFLOW raised");
}

# See also