Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::scalbn,std::scalbnf,std::scalbnl,std::scalbln,std::scalblnf,std::scalblnl

      From cppreference.com
      <cpp‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      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)
      Classification and comparison
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Types
      (C++11)
      (C++11)
      (C++11)
      Macro constants
       
      Defined in header<cmath>
      int exponent
      (1)
      float       scalbn(float num,int exp);

      double      scalbn(double num,int exp);

      longdouble scalbn(longdouble num,int exp);
      (since C++11)
      (until C++23)
      constexpr/* floating-point-type */
                  scalbn(/* floating-point-type */ num,int exp);
      (since C++23)
      float       scalbnf(float num,int exp);
      (2)(since C++11)
      (constexpr since C++23)
      longdouble scalbnl(longdouble num,int exp);
      (3)(since C++11)
      (constexpr since C++23)
      long exponent
      (4)
      float       scalbln(float num,long exp);

      double      scalbln(double num,long exp);

      longdouble scalbln(longdouble num,long exp);
      (since C++11)
      (until C++23)
      constexpr/* floating-point-type */
                  scalbln(/* floating-point-type */ num,long exp);
      (since C++23)
      float       scalblnf(float num,long exp);
      (5)(since C++11)
      (constexpr since C++23)
      longdouble scalblnl(longdouble num,long exp);
      (6)(since C++11)
      (constexpr since C++23)
      Defined in header<cmath>
      template<class Integer>
      double scalbn( Integer num,int exp);
      (A)(since C++11)
      (constexpr since C++23)
      template<class Integer>
      double scalbln( Integer num,long exp);
      (B)(since C++11)
      (constexpr since C++23)
      1-6) Multiplies a floating point valuenum byFLT_RADIX raised to powerexp. The library provides overloads ofstd::scalbn andstd::scalbln for all cv-unqualified floating-point types as the type of the parameternum.(since C++23)
      A,B) Additional overloads are provided for all integer types, which are treated asdouble.

      Contents

      [edit]Parameters

      num - floating-point or integer value
      exp - integer value

      [edit]Return value

      If no errors occur,num multiplied byFLT_RADIX to the power ofexp (num×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 inmath_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.
      • Ifnum is ±0, it is returned, unmodified.
      • Ifnum is ±∞, it is returned, unmodified.
      • Ifexp is 0, thennum is returned, unmodified.
      • Ifnum is NaN, NaN is returned.

      [edit]Notes

      On binary systems (whereFLT_RADIX is2),std::scalbn is equivalent tostd::ldexp.

      Althoughstd::scalbn andstd::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", wherescalb was an older non-standard function whose second argument had floating-point type.

      Thestd::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-guaranteedINT_MAX. In particular, for the 80-bitlongdouble, the factor is 32828.

      The GNU implementation does not seterrno regardless ofmath_errhandling.

      The additional overloads are not required to be provided exactly as(A,B). They only need to be sufficient to ensure that for their argumentnum of integer type:

      • std::scalbn(num, exp) has the same effect asstd::scalbn(static_cast<double>(num), exp).
      • std::scalbln(num, exp) has the same effect asstd::scalbln(static_cast<double>(num), exp).

      [edit]Example

      Run this code
      #include <cerrno>#include <cfenv>#include <cmath>#include <cstring>#include <iostream>// #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 valuesstd::cout<<"scalbn(-0, 10) = "<< std::scalbn(-0.0,10)<<'\n'<<"scalbn(-Inf, -1) = "<< std::scalbn(-INFINITY,-1)<<'\n'; // error handlingerrno=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.4375scalbn(1, -1074) = 4.94066e-324 (minimum positive subnormal double)scalbn(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double)scalbn(-0, 10) = -0scalbn(-Inf, -1) = -infscalbn(1, 1024) = inf    errno == ERANGE: Numerical result out of range    FE_OVERFLOW raised

      [edit]See also

      (C++11)(C++11)
      decomposes a number into significand and base-2 exponent
      (function)[edit]
      (C++11)(C++11)
      multiplies a number by2 raised to an integral power
      (function)[edit]
      C documentation forscalbn
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/scalbn&oldid=177738"

      [8]ページ先頭

      ©2009-2025 Movatter.jp