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

std::sqrt

Defined in header <cmath>

float sqrt( float arg );

(1)

double sqrt( double arg );

(2)

long double sqrt( long double arg );

(3)

double sqrt( Integral arg );

(4)

(since C++11)

的平方根。arg...

4%29一组过载或接受任意参数的函数模板积分型等于2%29%28double29%。

参数

arg

-

Value of a floating-point or Integral type

返回值

如果没有错误发生,则平方根为arg%28 XLII Arg%29,返回。

如果发生域错误,则返回支持%29的实现定义值%28 NaN。

如果由于下流而发生范围错误,则返回舍入%29后的正确结果%28。

错误处理

错误按math_errhandling...

在以下情况下发生域错误arg小于零。

如果实现支持ieee浮点算法%28IEC 60559%29,

  • 如果争论少于-0,FE_INVALID会被引发,NaN也会被返回。
  • 如果参数为+∞或±0,则返回参数,未修改。
  • 如果参数为nan,则返回nan。

注记

std::sqrt是IEEE标准所要求的。唯一需要精确的其他操作是算术算子和功能std::fma使用默认舍入模式%29舍入到返回类型%28后,std::sqrt与无限精确的结果是无法区分的。换句话说,错误小于0.5 up。其他职能,包括std::pow,没有那么受约束。

二次

代码语言:javascript
复制
#include <iostream>
#include <cmath>
#include <cerrno>
#include <cfenv>
#include <cstring>
 
#pragma STDC FENV_ACCESS ON
 
int main()
{
    // normal use
    std::cout << "sqrt(100) = " << std::sqrt(100) << '\n'
              << "sqrt(2) = " << std::sqrt(2) << '\n'
              << "golden ratio = " << (1+std::sqrt(5))/2 << '\n';
    // special values
    std::cout << "sqrt(-0) = " << std::sqrt(-0.0) << '\n';
    // error handling
    errno = 0; std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "sqrt(-1.0) = " << std::sqrt(-1) << '\n';
    if(errno == EDOM)
        std::cout << "    errno = EDOM " << std::strerror(errno) << '\n';
    if(std::fetestexcept(FE_INVALID))
        std::cout << "    FE_INVALID raised\n";
}

二次

可能的产出:

二次

代码语言:javascript
复制
sqrt(100) = 10
sqrt(2) = 1.41421
golden ratio = 1.61803
sqrt(-0) = -0
sqrt(-1.0) = -nan
    errno = EDOM Numerical argument out of domain
    FE_INVALID raised

二次

另见

pow

raises a number to the given power (xy) (function)

cbrt (C++11)

computes cubic root (3√x) (function)

hypot (C++11)

computes square root of the sum of the squares of two given numbers (√x2+y2) (function)

sqrt(std::complex)

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

sqrt(std::valarray)

applies the function std::sqrt to each element of valarray (function template)

c关于SQrt的文件

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

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

扫码关注腾讯云开发者

领取腾讯云代金券