命名空间
变体
操作

std::sqrt, std::sqrtf, std::sqrtl

来自 cppreference.cn
< cpp‎ | numeric‎ | math
 
 
 
常用数学函数
函数
基本运算
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指数函数
(C++11)
(C++11)

(C++11)
(C++11)
幂函数
sqrt
(C++11)
(C++11)
三角函数
双曲函数
(C++11)
(C++11)
(C++11)

误差和伽玛函数
(C++11)
(C++11)
(C++11)
(C++11)
最接近整数的浮点运算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮点数操作函数
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分类和比较
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型
(C++11)
(C++11)
(C++11)
宏常量
分类
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定义于头文件 <cmath>
(1)
float       sqrt ( float num );

double      sqrt ( double num );

long double sqrt ( long double num );
(直到 C++23)
/*floating-point-type*/
            sqrt ( /*floating-point-type*/ num );
(自 C++23 起)
(constexpr 自 C++26 起)
float       sqrtf( float num );
(2) (自 C++11 起)
(constexpr 自 C++26 起)
long double sqrtl( long double num );
(3) (自 C++11 起)
(constexpr 自 C++26 起)
SIMD 重载 (自 C++26 起)
定义于头文件 <simd>
template< /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/<V>

            sqrt ( const V& v_num );
(S) (自 C++26 起)
额外的重载 (自 C++11 起)
定义于头文件 <cmath>
template< class Integer >
double      sqrt ( Integer num );
(A) (constexpr 自 C++26 起)
1-3) 计算 num 的平方根。 库为所有 cv 限定的浮点类型作为参数类型提供了 std::sqrt 的重载。(自 C++23 起)
S) SIMD 重载对 v_num 执行元素级的 std::sqrt
(参见 math-floating-pointdeduced-simd-t 以了解其定义。)
(自 C++26 起)
A) 为所有整数类型提供了额外的重载,这些重载被视为 double
(自 C++11 起)

目录

[编辑] 参数

num - 浮点值或整数值

[编辑] 返回值

若无错误发生,则返回 num 的平方根 (num)。

若发生域错误,则返回实现定义的数值(若支持则为 NaN)。

若由于下溢而发生范围错误,则返回正确结果(舍入后)。

[编辑] 错误处理

错误报告方式如 math_errhandling 中所指定。

num 小于零,则发生域错误。

若实现支持 IEEE 浮点算术 (IEC 60559),

  • 若参数小于 -0,则引发 FE_INVALID 并返回 NaN。
  • 若参数为 +∞ 或 ±0,则原样返回,不修改。
  • 若参数为 NaN,则返回 NaN。

[编辑] 注解

IEEE 标准要求 std::sqrt 从无限精确的结果正确舍入。特别是,如果精确结果可以用浮点类型表示,则会生成该结果。唯一其他需要这样做的运算是算术运算符和函数 std::fma。其他函数,包括 std::pow,则不受此约束。

额外的重载不一定需要完全按照 (A) 的方式提供。它们只需要足以确保对于整数类型的参数 numstd::sqrt(num) 具有与 std::sqrt(static_cast<double>(num)) 相同的效果。

[编辑] 示例

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #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";
}

可能的输出

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

[编辑] 参见

(C++11)(C++11)
将数字提升到给定的幂次 (xy)
(函数) [编辑]
(C++11)(C++11)(C++11)
计算立方根 (3x)
(函数) [编辑]
(C++11)(C++11)(C++11)
计算斜边 x2
+y2
x2
+y2
+z2
(自 C++17 起)

(函数) [编辑]
复数平方根,范围为右半平面
(函数模板) [编辑]
将函数 std::sqrt 应用于 valarray 的每个元素
(函数模板) [编辑]
C 文档 关于 sqrt