首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::sqrt(std::valarray)

Defined in header <valarray>

template< class T > valarray<T> sqrt( const valarray<T>& va );

中的每个元素va计算元素值的平方根。

参数

va

-

value array to apply the operation to

返回值

中的值的平方根的值数组。va...

注记

不合格职能%28sqrt%29用于执行计算。如果没有这样的功能,std::sqrt由于参数相关查找而使用。

函数的返回类型与std::valarray在这种情况下,替换类型具有以下属性:

  • const成员职能std::valarray提供。
  • std::valarray,,,std::slice_array,,,std::gslice_array,,,std::mask_arraystd::indirect_array可以从替换类型构造。
  • 所有接受类型参数的函数conststd::valarray&begin()end()%28,因为C++14%29也应该接受替换类型。
  • 接受两个类型参数的所有函数conststd::valarray&应该接受每一个组合conststd::valarray&以及替代型。
  • 返回类型不会在最嵌套的参数类型上添加两个以上的模板嵌套级别。

可能的实施

模板<class T>值阵<T>SQrt%28康复阀阵列<T>&va%29{valArray<T>Other=va;for%28t&i:其他%29{i=sqrt%28i%29;}返回Other;}

*。

找到多个二次方程的实根。

二次

代码语言:javascript
复制
#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";
    }
}

二次

产出:

二次

代码语言:javascript
复制
quadratic equation    root 1,  root 2
1x² + 1x + -1 = 0   -1.61803, 0.618034
1x² + 2x + -2 = 0   -2.73205, 0.732051
1x² + 3x + -3 = 0   -3.79129, 0.791288
1x² + 4x + -4 = 0   -4.82843, 0.828427
1x² + 5x + -5 = 0   -5.8541, 0.854102
1x² + 6x + -6 = 0   -6.87298, 0.872983
1x² + 7x + -7 = 0   -7.88748, 0.887482
1x² + 8x + -8 = 0   -8.89898, 0.898979

二次

另见

pow(std::valarray)

applies the function std::pow to two valarrays or a valarray and a value (function template)

sqrt

computes square root (√x) (function)

sqrt(std::complex)

complex square root in the range of the right half-plane (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券