Namespaces
Variants
Views
Actions

std::scalbn, std::scalbln

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Exponential functions
(C++11)
(C++11)
(C++11)
(C++11)
Power functions
(C++11)
(C++11)
Trigonometric and hyperbolic functions
(C++11)
(C++11)
(C++11)
Error and gamma functions
(C++11)
(C++11)
(C++11)
(C++11)
Nearest integer floating point operations
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
Floating point manipulation functions
scalbnscalbln
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
Classification/Comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Macro constants
(C++11)(C++11)(C++11)(C++11)(C++11)
 
Defined in header <cmath>
float       scalbn( float x, int exp );
(1) (since C++11)
double      scalbn( double x, int exp );
(2) (since C++11)
long double scalbn( long double x, int exp );
(3) (since C++11)
double      scalbn( Integral x, int exp );
(4) (since C++11)
float       scalbln( float x, long exp );
(5) (since C++11)
double      scalbln( double x, long exp );
(6) (since C++11)
long double scalbln( long double x, long exp );
(7) (since C++11)
double      scalbln( Integral x, long exp );
(8) (since C++11)
1-3,5-7) Multiplies a floating point value x by FLT_RADIX raised to power exp.
4,8) A set of overloads or a function template accepting an argument of any integral type. Equivalent to (2) or (6) (the argument is cast to double).

Contents

[edit] Parameters

x - floating point value
exp - integer value

[edit] Return value

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

If a range error due to overflow occurs, ±HUGE_VAL, ±HUGE_VALF, or ±HUGE_VALL is returned.

If a range error due to underflow occurs, the correct result (after rounding) is returned.

[edit] Error handling

Errors are reported as specified in math_errhandling

If the implementation supports IEEE floating-point arithmetic (IEC 60559),

  • Unless a range error occurs, FE_INEXACT is never raised (the result is exact)
  • Unless a range error occurs, the current rounding mode is ignored
  • If x is ±0, it is returned, unmodified
  • If x is ±∞, it is returned, unmodified
  • If exp is 0, then x is returned, unmodified
  • If x is NaN, NaN is returned

[edit] Notes

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

Although std::scalbn and std::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 function name stands for "new scalb", where scalb was an older non-standard function whose second argument had floating-point type.

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.

The GNU implementation does not set errno regardless of math_errhandling

[edit] Example

#include <iostream>
#include <cmath>
#include <cerrno>
#include <cstring>
#include <cfenv>
 
#pragma STDC FENV_ACCESS ON
int main()
{
    std::cout << "scalbn(7, -4) = " << std::scalbn(7, -4) << '\n'
              << "scalbn(1, -1074) = " << std::scalbn(1, -1074)
              << " (minimum positive subnormal double)\n"
              << "scalbn(nextafter(1,0), 1024) = "
              << std::scalbn(std::nextafter(1,0), 1024)
              << " (largest finite double)\n";
    // special values
    std::cout << "scalbn(-0, 10) = " << std::scalbn(-0.0, 10) << '\n'
              << "scalbn(-Inf, -1) = " << std::scalbn(-INFINITY, -1) << '\n';
    // error handling
    errno=0; std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "scalbn(1, 1024) = " << std::scalbn(1, 1024) << '\n';
    if(errno == ERANGE)
        std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
    if(std::fetestexcept(FE_OVERFLOW))
        std::cout << "    FE_OVERFLOW raised\n";
}

Possible output:

scalbn(7, -4) = 0.4375
scalbn(1, -1074) = 4.94066e-324 (minimum positive subnormal double)
scalbn(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double)
scalbn(-0, 10) = -0
scalbn(-Inf, -1) = -inf
scalbn(1, 1024) = inf
    errno == ERANGE: Numerical result out of range
    FE_OVERFLOW raised

[edit] See also

decomposes a number into significand and a power of 2
(function) [edit]
multiplies a number by 2 raised to a power
(function) [edit]
C documentation for scalbn