Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::pow,std::powf,std::powl

      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
      (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       pow(float base,float exp);

      double      pow(double base,double exp);

      longdouble pow(longdouble base,longdouble exp);
      (until C++23)
      /* floating-point-type */

                  pow(/* floating-point-type */ base,

                       /* floating-point-type */ exp)
      (since C++23)
      (constexpr since C++26)
      float       pow(float base,int exp);

      double      pow(double base,int exp);

      longdouble pow(longdouble base,int exp);
      (2)(until C++11)
      float       powf(float base,float exp);
      (3)(since C++11)
      (constexpr since C++26)
      longdouble powl(longdouble base,longdouble exp);
      (4)(since C++11)
      (constexpr since C++26)
      Defined in header<cmath>
      template<class Arithmetic1,class Arithmetic2>

      /* common-floating-point-type */

                  pow( Arithmetic1 base, Arithmetic2 exp);
      (A)(constexpr since C++26)
      1-4) Computes the value ofbase raised to the powerexp. The library provides overloads ofstd::pow for all cv-unqualified floating-point types as the type of the parametersbase andexp.(since C++23)
      A) Additional overloads are provided for all other combinations of arithmetic types.
      (since C++11)

      Contents

      [edit]Parameters

      base - base as a floating-point or integer value
      exp - exponent as a floating-point or integer value

      [edit]Return value

      If no errors occur,base raised to the power ofexp (baseexp
      ), is returned.

      If a domain error occurs, an implementation-defined value is returned (NaN where supported).

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

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

      [edit]Error handling

      Errors are reported as specified inmath_errhandling.

      Ifbase is finite and negative andexp is finite and non-integer, a domain error occurs and a range error may occur.

      Ifbase is zero andexp is zero, a domain error may occur.

      Ifbase is zero andexp is negative, a domain error or a pole error may occur.

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

      • pow(+0, exp), whereexp is a negative odd integer, returns +∞ and raisesFE_DIVBYZERO.
      • pow(-0, exp), whereexp is a negative odd integer, returns -∞ and raisesFE_DIVBYZERO.
      • pow(±0, exp), whereexp is negative, finite, and is an even integer or a non-integer, returns +∞ and raisesFE_DIVBYZERO.
      • pow(±0,-) returns +∞ and may raiseFE_DIVBYZERO.
      • pow(+0, exp), whereexp is a positive odd integer, returns +0.
      • pow(-0, exp), whereexp is a positive odd integer, returns -0.
      • pow(±0, exp), whereexp is positive non-integer or a positive even integer, returns +0.
      • pow(-1, ±∞) returns 1.
      • pow(+1, exp) returns 1 for anyexp, even whenexp is NaN.
      • pow(base, ±0) returns 1 for anybase, even whenbase is NaN.
      • pow(base, exp) returns NaN and raisesFE_INVALID ifbase is finite and negative andexp is finite and non-integer.
      • pow(base,-) returns +∞ for any|base| < 1.
      • pow(base,-) returns +0 for any|base| > 1.
      • pow(base,+) returns +0 for any|base| < 1.
      • pow(base,+) returns +∞ for any|base| > 1.
      • pow(-∞, exp) returns -0 ifexp is a negative odd integer.
      • pow(-∞, exp) returns +0 ifexp is a negative non-integer or negative even integer.
      • pow(-∞, exp) returns -∞ ifexp is a positive odd integer.
      • pow(-∞, exp) returns +∞ ifexp is a positive non-integer or positive even integer.
      • pow(+∞, exp) returns +0 for any negativeexp.
      • pow(+∞, exp) returns +∞ for any positiveexp.
      • except where specified above, if any argument is NaN, NaN is returned.

      [edit]Notes

      C++98 added overloads whereexp has typeint on top of Cpow(), and the return type ofstd::pow(float,int) wasfloat. However, the additional overloads introduced in C++11 specify thatstd::pow(float,int) should returndouble.LWG issue 550 was raised to target this conflict, and the resolution is to removed the extraintexp overloads.

      Althoughstd::pow cannot be used to obtain a root of a negative number,std::cbrt is provided for the common case whereexp is 1/3.

      The additional overloads are not required to be provided exactly as(A). They only need to be sufficient to ensure that for their first argumentnum1 and second argumentnum2:

      • Ifnum1 ornum2 has typelongdouble, thenstd::pow(num1, num2) has the same effect asstd::pow(static_cast<longdouble>(num1),
                 static_cast<longdouble>(num2))
        .
      • Otherwise, ifnum1 and/ornum2 has typedouble or an integer type, thenstd::pow(num1, num2) has the same effect asstd::pow(static_cast<double>(num1),
                 static_cast<double>(num2))
        .
      • Otherwise, ifnum1 ornum2 has typefloat, thenstd::pow(num1, num2) has the same effect asstd::pow(static_cast<float>(num1),
                 static_cast<float>(num2))
        .
      (until C++23)

      Ifnum1 andnum2 have arithmetic types, thenstd::pow(num1, num2) has the same effect asstd::pow(static_cast</*common-floating-point-type*/>(num1),
               static_cast</*common-floating-point-type*/>(num2))
      , where/*common-floating-point-type*/ is the floating-point type with the greatestfloating-point conversion rank and greatestfloating-point conversion subrank between the types ofnum1 andnum2, arguments of integer type are considered to have the same floating-point conversion rank asdouble.

      If no such floating-point type with the greatest rank and subrank exists, thenoverload resolution does not result in a usable candidate from the overloads provided.

      (since C++23)

      [edit]Example

      Run this code
      #include <cerrno>#include <cfenv>#include <cmath>#include <cstring>#include <iostream>// #pragma STDC FENV_ACCESS ON int main(){// typical usagestd::cout<<"pow(2, 10) = "<< std::pow(2,10)<<'\n'<<"pow(2, 0.5) = "<< std::pow(2,0.5)<<'\n'<<"pow(-2, -3) = "<< std::pow(-2,-3)<<'\n'; // special valuesstd::cout<<"pow(-1, NAN) = "<< std::pow(-1,NAN)<<'\n'<<"pow(+1, NAN) = "<< std::pow(+1,NAN)<<'\n'<<"pow(INFINITY, 2) = "<< std::pow(INFINITY,2)<<'\n'<<"pow(INFINITY, -1) = "<< std::pow(INFINITY,-1)<<'\n'; // error handlingerrno=0;std::feclearexcept(FE_ALL_EXCEPT); std::cout<<"pow(-1, 1/3) = "<< std::pow(-1,1.0/3)<<'\n';if(errno==EDOM)std::cout<<"    errno == EDOM "<<std::strerror(errno)<<'\n';if(std::fetestexcept(FE_INVALID))std::cout<<"    FE_INVALID raised\n"; std::feclearexcept(FE_ALL_EXCEPT); std::cout<<"pow(-0, -3) = "<< std::pow(-0.0,-3)<<'\n';if(std::fetestexcept(FE_DIVBYZERO))std::cout<<"    FE_DIVBYZERO raised\n";}

      Possible output:

      pow(2, 10) = 1024pow(2, 0.5) = 1.41421pow(-2, -3) = -0.125pow(-1, NAN) = nanpow(+1, NAN) = 1pow(INFINITY, 2) = infpow(INFINITY, -1) = 0pow(-1, 1/3) = -nan    errno == EDOM Numerical argument out of domain    FE_INVALID raisedpow(-0, -3) = -inf    FE_DIVBYZERO raised

      [edit]See also

      (C++11)(C++11)
      computes square root (\(\small{\sqrt{x}}\)x)
      (function)[edit]
      (C++11)(C++11)(C++11)
      computes cube root (\(\small{\sqrt[3]{x}}\)3x)
      (function)[edit]
      (C++11)(C++11)(C++11)
      computes hypotenuse\(\scriptsize{\sqrt{x^2+y^2}}\)x2
      +y2
      and\(\scriptsize{\sqrt{x^2+y^2+z^2}}\)x2
      +y2
      +z2
      (since C++17)

      (function)[edit]
      complex power, one or both arguments may be a complex number
      (function template)[edit]
      applies the functionstd::pow to two valarrays or a valarray and a value
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/pow&oldid=177733"

      [8]ページ先頭

      ©2009-2025 Movatter.jp