Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::pow(std::valarray)

      From cppreference.com
      <cpp‎ |numeric‎ |valarray
       
       
       
      std::valarray
      Member functions
      Non-member functions
      Helper classes
      Deduction guides(C++17)
       
      Defined in header<valarray>
      template<class T>
      std::valarray<T> pow(conststd::valarray<T>& base,conststd::valarray<T>& exp);
      (1)
      template<class T>

      std::valarray<T> pow(conststd::valarray<T>& base,

                           consttypenamestd::valarray<T>::value_type& vexp);
      (2)
      template<class T>

      std::valarray<T> pow(consttypenamestd::valarray<T>::value_type& vbase,

                           conststd::valarray<T>& exp);
      (3)

      Raises a value to a power.

      1) Computes the values of each element in the numeric arraybase raised to the power specified by the corresponding element from the numeric arrayexp.

      The behavior is undefined ifbase.size()!= exp.size().

      2) Computes the values of each element in the numeric arraybase raised to the powervexp.
      3) Computes the values ofvbase raised to the power defined by the elements in the numeric arrayexp.

      Contents

      [edit]Parameters

      base - numeric array containing the values of the base
      exp - numeric array containing the values of the exponent
      vbase - a value defining the base
      vexp - a value defining the exponent

      [edit]Return value

      A numeric array containing the results of exponentiation.

      [edit]Notes

      Unqualified function (pow) is used to perform the computation. If such function is not available,std::pow is used due toargument-dependent lookup.

      The function can be implemented with the return type different fromstd::valarray. In this case, the replacement type has the following properties:

      [edit]Example

      Run this code
      #include <cmath>#include <cstddef>#include <iomanip>#include <iostream>#include <valarray> class show{friendstd::ostream& operator<<(std::ostream& os, showconst& r){constexprcharconst* sup[]{"\u2070","\u00B9","\u00B2","\u00B3","\u2074","\u2075","\u2076","\u2077","\u2078","\u2079"}; for(std::size_t n=0; n!= r.bases.size();++n){            os<<std::left<< r.bases[n]<<std::left;if(n< r.exponents.size())                os<< sup[r.exponents[n]%10]<<' ';else                os<<"  ";} if(r.results.size()!=0){            os<<'=';for(std::size_t n=0; n!= r.results.size();++n)                os<<' '<< r.results[n];} return os<<'\n';} public:std::valarray<int> bases{}, exponents{}, results{};}; int main(){constexprint base{2};constexprint exponent{5};conststd::valarray<int> bases{1,2,3,4,5,6,7};conststd::valarray<int> exponents{0,1,2,3,4,5,6};conststd::valarray<int> powers1=std::pow(bases, exponents);conststd::valarray<int> powers2=std::pow(bases, exponent);conststd::valarray<int> powers3=std::pow(base, exponents); std::cout<<"pow(const std::valarray<T>& base, const std::valarray<T>& exp); (1)\n"<<"base : "<< show{bases}<<"exp  : "<< show{exponents}<<"pow  : "<< show{bases, exponents, powers1}<<'\n'<<"pow(const std::valarray<T>& base, const value_type& vexp); (2)\n"<<"base : "<< show{bases}<<"vexp : "<< exponent<<'\n'<<"pow  : "<< show{bases,std::valarray<int>(exponent, bases.size()), powers2}<<'\n'<<"pow(const value_type& vbase, const std::valarray<T>& exp); (3)\n"<<"vbase: "<< base<<'\n'<<"exp  : "<< show{exponents}<<"pow  : "<< show{std::valarray<int>(base, bases.size()), exponents, powers3};}

      Output:

      pow(const std::valarray<T>& base, const std::valarray<T>& exp); (1)base : 1  2  3  4  5  6  7exp  : 0  1  2  3  4  5  6pow  : 1⁰ 2¹ 3² 4³ 5⁴ 6⁵ 7⁶ = 1 2 9 64 625 7776 117649 pow(const std::valarray<T>& base, const value_type& vexp); (2)base : 1  2  3  4  5  6  7vexp : 5pow  : 1⁵ 2⁵ 3⁵ 4⁵ 5⁵ 6⁵ 7⁵ = 1 32 243 1024 3125 7776 16807 pow(const value_type& vbase, const std::valarray<T>& exp); (3)vbase: 2exp  : 0  1  2  3  4  5  6pow  : 2⁰ 2¹ 2² 2³ 2⁴ 2⁵ 2⁶ = 1 2 4 8 16 32 64

      [edit] Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3074C++98T is deduced from both the scalar and thevalarray for(2,3), disallowing mixed-type callsonly deduceT from thevalarray

      [edit]See also

      applies the functionstd::sqrt to each element of valarray
      (function template)[edit]
      (C++11)(C++11)
      raises a number to the given power (\(\small{x^y}\)xy)
      (function)[edit]
      complex power, one or both arguments may be a complex number
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/valarray/pow&oldid=160828"

      [8]ページ先頭

      ©2009-2025 Movatter.jp