function template
<valarray>

std::sqrt

template<class T> valarray<T> sqrt (const valarray<T>& x);
Compute square root of valarray elements
Returns a valarray containing the square root of all the elements of x, in the same order.

The function calls sqrt (unqualified) once for each element.

This function overloads cmath's sqrt.

Parameters

x
valarray containing elements of a type for which the unary function sqrt is defined.

Return value

A valarray object with the square roots of the elements of x.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// sqrt valarray example
#include <iostream>     // std::cout
#include <cstddef>      // std::size_t
#include <cmath>        // std::sqrt(double)
#include <valarray>     // std::valarray, std::sqrt(valarray)

int main ()
{
  double val[] = {9.0, 25.0, 100.0};
  std::valarray<double> foo (val,3);

  std::valarray<double> bar = sqrt (foo);

  std::cout << "foo:";
  for (std::size_t i=0; i<foo.size(); ++i)
    std::cout << ' ' << foo[i];
  std::cout << '\n';

  std::cout << "bar:";
  for (std::size_t i=0; i<bar.size(); ++i)
    std::cout << ' ' << bar[i];
  std::cout << '\n';

  return 0;
}


Output:

foo: 9 25 100
bar: 3 5 10

See also