Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ldexp,std::ldexpf,std::ldexpl

      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
      ldexp
      (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>
      (1)
      float       ldexp(float num,int exp);

      double      ldexp(double num,int exp);

      longdouble ldexp(longdouble num,int exp);
      (until C++23)
      constexpr/* floating-point-type */
                  ldexp(/* floating-point-type */ num,int exp);
      (since C++23)
      float       ldexpf(float num,int exp);
      (2)(since C++11)
      (constexpr since C++23)
      longdouble ldexpl(longdouble num,int exp);
      (3)(since C++11)
      (constexpr since C++23)
      Defined in header<cmath>
      template<class Integer>
      double      ldexp( Integer num,int exp);
      (A)(since C++11)
      (constexpr since C++23)
      1-3) Multiplies a floating point valuenum by the number2 raised to theexp power. The library provides overloads ofstd::ldexp for all cv-unqualified floating-point types as the type of the parameternum.(since C++23)
      A) Additional overloads are provided for all integer types, which are treated asdouble.
      (since C++11)

      Contents

      [edit]Parameters

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

      [edit]Return value

      If no errors occur,num multiplied by 2 to the power ofexp (num×2exp
      ) 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::ldexp is equivalent tostd::scalbn.

      The functionstd::ldexp ("load exponent"), together with its dual,std::frexp, can be used to manipulate the representation of a floating-point number without direct bit manipulations.

      On many implementations,std::ldexp is less efficient than multiplication or division by a power of two using arithmetic operators.

      The additional overloads are not required to be provided exactly as(A). They only need to be sufficient to ensure that for their argumentnum of integer type,std::ldexp(num, exp) has the same effect asstd::ldexp(static_cast<double>(num), exp).

      For exponentiation of 2 by a floating point exponent,std::exp2 can be used.

      [edit]Example

      Run this code
      #include <cerrno>#include <cfenv>#include <cmath>#include <cstring>#include <iostream>// #pragma STDC FENV_ACCESS ON int main(){std::cout<<"ldexp(5, 3) = 5 * 8 = "<< std::ldexp(5,3)<<'\n'<<"ldexp(7, -4) = 7 / 16 = "<< std::ldexp(7,-4)<<'\n'<<"ldexp(1, -1074) = "<< std::ldexp(1,-1074)<<" (minimum positive subnormal float64_t)\n"<<"ldexp(nextafter(1,0), 1024) = "<< std::ldexp(std::nextafter(1,0),1024)<<" (largest finite float64_t)\n"; // special valuesstd::cout<<"ldexp(-0, 10) = "<< std::ldexp(-0.0,10)<<'\n'<<"ldexp(-Inf, -1) = "<< std::ldexp(-INFINITY,-1)<<'\n'; // error handlingstd::feclearexcept(FE_ALL_EXCEPT);errno=0;constdouble inf= std::ldexp(1,1024);constbool is_range_error=errno==ERANGE; std::cout<<"ldexp(1, 1024) = "<< inf<<'\n';if(is_range_error)std::cout<<"    errno == ERANGE: "<<std::strerror(ERANGE)<<'\n';if(std::fetestexcept(FE_OVERFLOW))std::cout<<"    FE_OVERFLOW raised\n";}

      Possible output:

      ldexp(5, 3) = 5 * 8 = 40ldexp(7, -4) = 7 / 16 = 0.4375ldexp(1, -1074) = 4.94066e-324 (minimum positive subnormal float64_t)ldexp(nextafter(1,0), 1024) = 1.79769e+308 (largest finite float64_t)ldexp(-0, 10) = -0ldexp(-Inf, -1) = -infldexp(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)(C++11)(C++11)(C++11)(C++11)
      multiplies a number byFLT_RADIX raised to a power
      (function)[edit]
      (C++11)(C++11)(C++11)
      returns2 raised to the given power (\({\small 2^x}\)2x)
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/ldexp&oldid=177736"

      [8]ページ先頭

      ©2009-2025 Movatter.jp