sqrt, sqrtf, sqrtl

计算平方根。

语法

double sqrt(
   double x
);
float sqrt(
   float x
);  // C++ only
long double sqrt(
   long double x
);  // C++ only
float sqrtf(
   float x
);
long double sqrtl(
   long double x
);
#define sqrt(x) // Requires C11 or higher

参数

x
非负浮点值

备注

由于 C++ 允许重载,因此你可以调用采用 sqrtfloat 类型的 long double 重载。 在 C 程序中,除非你使用 <tgmath.h> 宏来调用此函数,否则 sqrt 始终接受并返回 double

如果使用 <tgmath.h> sqrt() 宏,参数的类型将确定选择哪个版本的函数。 有关详细信息,请参阅泛型类型数学

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

返回值

sqrt 函数返回 x 的平方根。 默认情况下,如果 x 为负,则 sqrt 将返回不定值 NaN

输入 SEH 异常 _matherr 异常
± QNaN、IND _DOMAIN
- INF _DOMAIN
x < 0 _DOMAIN

要求

功能 C 标头 C++ 标头
sqrt, sqrtf, sqrtl <math.h> <cmath>
sqrt <tgmath.h>

有关兼容性信息,请参阅兼容性

示例

// crt_sqrt.c
// This program calculates a square root.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   double question = 45.35, answer;
   answer = sqrt( question );
   if( question < 0 )
      printf( "Error: sqrt returns %f\n", answer );
   else
      printf( "The square root of %.2f is %.2f\n", question, answer );
}
The square root of 45.35 is 6.73

另请参阅

数学和浮点支持
expexpfexpl
loglogflog10log10f
powpowfpowl
_CIsqrt