C++ <valarray> - sqrt() Function
The C++ <valarray> sqrt() function returns a valarray containing square root value of all the elements of valarray va, in the same order. This function overloads with cmath's sqrt() function and calls this function once for each element.
Syntax
template<class T> valarray<T> sqrt (const valarray<T>& va);
Parameters
va |
Specify valarray containing elements of a type for which the function sqrt() is defined. |
Return Value
Returns valarray containing square root value of all the elements of va.
Example:
The example below shows the usage of sqrt() function.
#include <iostream> #include <valarray> using namespace std; int main (){ valarray<double> va1 = {4, 9, 16, 50}; //va2 will contain square root //of all elements of va1 valarray<double> va2 = sqrt(va1); cout<<fixed; cout<<"va1 contains: "; for(int i = 0; i < va1.size(); i++) cout<<va1[i]<<" "; cout<<"\nva2 contains: "; for(int i = 0; i < va2.size(); i++) cout<<va2[i]<<" "; return 0; }
The output of the above code will be:
va1 contains: 4.000000 9.000000 16.000000 50.000000 va2 contains: 2.000000 3.000000 4.000000 7.071068
❮ C++ <valarray> Library