std::sqrt(std::valarray) (3) Linux Manual Page
std::sqrt(std::valarray) – std::sqrt(std::valarray)
Synopsis
Defined in header<valarray>
template <class T>
valarray<T> sqrt(const valarray<T> &va);
For each element in va computes the square root of the value of the element.
Parameters
va – value array to apply the operation to
Return value
Value array containing square roots of the values in va.
Notes
Unqualified function (sqrt) is used to perform the computation. If such function is not available, std::sqrt 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:
Possible implementation
Example
Finds real roots of multiple quadratic equations.
// Run this code
#include <valarray>
#include <iostream>
int main()
{
std::valarray<double> a(1, 8);
std::valarray<double> b{1, 2, 3, 4, 5, 6, 7, 8};
std::valarray<double> c = -b;
// literals must also be of type T (double in this case)
std::valarray<double> d = std::sqrt((b * b - 4.0 * a * c));
std::valarray<double> x1 = (-b - d) / (2.0 * a);
std::valarray<double> x2 = (-b + d) / (2.0 * a);
std::cout << "quadratic equation root 1, root 2"
<< "\n";
for (size_t i = 0; i < a.size(); ++i) {
std::cout << a[i] << "x\u00B2 + " << b[i] << "x + " << c[i] << " = 0 ";
std::cout << x1[i] << ", " << x2[i] << "\n";
}
}
Output:
See also
pow(std::valarray) (function template)
sqrt
sqrtf computes square root (
sqrtl √
(C++11)
(C++11)
sqrt(std::complex) (function template)
