std::pow(std::valarray)(3)
π std::pow(std::valarray) π
okay so bestie, welcome to the C++ Standard Libary manual page for std::pow(std::valarray) β yes the typo βLibaryβ is canon, we donβt fix it π. This is the page where std::pow gets the valarray treatment, and itβs lowkey iconic.
πΈ NAME πΈ
std::pow(std::valarray) - std::pow(std::valarray)
β¨ Synopsis β¨
Defined in header <valarray>.
template< class T >
std::valarray<T> pow( const std::valarray<T>& base, const std::valarray<T>& exp ); (1)
template< class T >
std::valarray<T> pow( const std::valarray<T>& base, const typename std::valarray<T>::value_type& vexp ); (2)
template< class T >
std::valarray<T> pow( const typename std::valarray<T>::value_type& vbase, const std::valarray<T>& exp ); (3)
Raises a value to a power. Slay. β¨
- Computes the values of each element in the numeric array
baseraised to the power specified by the corresponding element from the numeric arrayexp.
The behavior is undefined if base.size() != exp.size(). π Translation: matching sizes or itβs UB, no cap.
- Computes the values of each element in the numeric array
baseraised to the powervexp. - Computes the values of
vbaseraised to the power defined by the elements in the numeric arrayexp.
π Parameters π
baseβ numeric array containing the values of the baseexpβ numeric array containing the values of the exponentvbaseβ a value defining the basevexpβ a value defining the exponent
π¦ Return value π¦
A numeric array containing the results of exponentiation. Thatβs the whole vibe. β¨
π Notes π
Unqualified function (pow) is used to perform the computation. If such function is not available, std::pow is used due to argument-dependent lookup.
The function can be implemented with the return type different from std::valarray. In this case, the replacement type has the following properties:
- All const member functions of
std::valarrayare provided. std::valarray,std::slice_array,std::gslice_array,std::mask_arrayandstd::indirect_arraycan be constructed from the replacement type.- For every function taking a
const std::valarray<T>&exceptbegin()andend()(since C++11), identical functions taking the replacement types shall be added; - For every function taking two
const std::valarray<T>&arguments, identical functions taking every combination ofconst std::valarray<T>&and replacement types shall be added. - The return type does not add more than two levels of template nesting over the most deeply-nested argument type.
π§ Example π§
// Run this code β yes bestie, run it, itβs cute π»
#include <cmath>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <valarray>
class show
{
friend std::ostream& operator<<(std::ostream& os, show const& r)
{
constexpr char const* 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()
{
constexpr int base{2};
constexpr int exponent{5};
const std::valarray<int> bases{1, 2, 3, 4, 5, 6, 7};
const std::valarray<int> exponents{0, 1, 2, 3, 4, 5, 6};
const std::valarray<int> powers1 = std::pow(bases, exponents);
const std::valarray<int> powers2 = std::pow(bases, exponent);
const std::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 7
exp : 0 1 2 3 4 5 6
pow : 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 7
vexp : 5
pow : 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: 2
exp : 0 1 2 3 4 5 6
pow : 2β° 2ΒΉ 2Β² 2Β³ 2β΄ 2β΅ 2βΆ = 1 2 4 8 16 32 64
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 3074β applied toC++98; behavior as published:Tis deduced from both the scalar and thevalarrayfor (2,3), disallowing mixed-type calls; correct behavior: only deduceTfrom thevalarray.
π See also π
sqrt(std::valarray)β applies the functionstd::sqrtto each element ofvalarray(function template)pow,powf,powl(C++11) (C++11) β raises a number to the given power ((\small{x^y})x^y) (function)pow(std::complex)β complex power, one or both arguments may be a complex number (function template)
π΅οΈ Hidden category π΅οΈ
- Pages with unreviewed LWG DR marker
thatβs the tea, bestie βπ source: http://cppreference.com, version date 2024.06.10, page std::pow(std::valarray)(3). xoxo, your C++ bestie π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms