Namespaces
Variants

sqrt, sqrtf, sqrtl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
sqrt
(C99)
(C23)
(C23)

Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <math.h>
float sqrtf ( float arg ) ;
(1) (C99 起)
double sqrt ( double arg ) ;
(2)
long double sqrtl ( long double arg ) ;
(3) (C99 起)
定义于头文件 <tgmath.h>
#define sqrt( arg )
(4) (C99 起)
1-3) 计算 arg 的平方根。
4) 类型泛型宏:若 arg 具有 long double 类型,则调用 sqrtl 。否则,若 arg 具有整数类型或 double 类型,则调用 sqrt 。否则调用 sqrtf 。若 arg 为复数或虚数,则宏将调用对应的复数函数( csqrtf csqrt csqrtl )。

目录

参数

arg - 浮点数值

返回值

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

如果发生定义域错误,则返回一个由实现定义的值(在支持 NaN 的情况下返回 NaN)。

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

错误处理

错误报告方式遵循 math_errhandling 中的规范。

arg 小于零时会发生定义域错误。

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

  • 若参数小于 -0,将引发 FE_INVALID 并返回 NaN。
  • 若参数为 +∞ 或 ±0,则直接返回原值。
  • 若参数为 NaN,则返回 NaN。

注释

sqrt 函数根据 IEEE 标准要求必须从无限精确的结果进行正确舍入。特别地,如果精确结果可以用浮点类型表示,则必须产生该精确结果。唯一其他有此要求的是 算术运算符 和函数 fma 。其他函数(包括 pow )则不受此约束。

示例

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
    // 常规用法
    printf("sqrt(100) = %f\n", sqrt(100));
    printf("sqrt(2) = %f\n", sqrt(2));
    printf("golden ratio = %f\n", (1 + sqrt(5)) / 2);
    // 特殊值
    printf("sqrt(-0) = %f\n", sqrt(-0.0));
    // 错误处理
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("sqrt(-1.0) = %f\n", sqrt(-1));
    if (errno == EDOM)
        perror("    errno == EDOM");
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID was raised");
}

可能的输出:

sqrt(100) = 10.000000
sqrt(2) = 1.414214
golden ratio = 1.618034
sqrt(-0) = -0.000000
sqrt(-1.0) = -nan
    errno = EDOM: Numerical argument out of domain
    FE_INVALID was raised

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.7.5 sqrt 函数集 (p: 待定)
  • 7.25 类型泛型数学 <tgmath.h> (p: 待定)
  • F.10.4.5 sqrt 函数集 (p: 待定)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.7.5 sqrt 函数集 (p: TBD)
  • 7.25 类型泛型数学 <tgmath.h> (p: TBD)
  • F.10.4.5 sqrt 函数集 (p: TBD)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.7.5 sqrt 函数 (第 249 页)
  • 7.25 泛型数学 <tgmath.h> (第 373-375 页)
  • F.10.4.5 sqrt 函数 (第 525 页)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.7.5 sqrt函数(页码:229-230)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.4.5 sqrt函数(页码:462)
  • C89/C90 标准 (ISO/IEC 9899:1990):
  • 4.5.5.2 sqrt 函数

另请参阅

(C99) (C99)
计算一个数的指定次幂( x y
(函数)
(C99) (C99) (C99)
计算立方根( 3 x
(函数)
(C99) (C99) (C99)
计算两数平方和的平方根( x 2
+y 2

(函数)
(C99) (C99) (C99)
计算复数的平方根
(函数)
C++ 文档 关于 sqrt