#C 语言标准库宏 sqrt
下面以函数形式说明宏的调用接口。
#include <tgmath.h>
generic_result sqrt(generic_value x);
说明
计算非负实数的主平方根。 示例的实参类型选择 sqrtf。
输入小于零时发生定义域错误。
适用的二进制实浮点类型和整数参与分派。含 long double 时选长双精度版本,否则含 double 或整数时选双精度版本,其余选择 float 版本。参与分派的参数中含复数时选择复数版本。 实现支持十进制重载时,不能把其参与分派的十进制实参和二进制浮点或复数实参混用。
自 C99 起提供。
参数
x- 被开方数。
返回值
- 返回非负平方根。在 IEC 60559 实现中保留零的符号。
#示例
使用 GCC 时,在链接命令末尾添加 -lm。
#include <tgmath.h>
#include <stdio.h>
int main(void)
{
float computed = sqrt(9.0f);
printf("%Lg\n", (long double)computed);
return !(computed == 3.0);
}
运行结果。
3
#相关接口
- 类型通用分派 csqrt、csqrtf、csqrtl、sqrt、sqrtl、sqrtd32、sqrtd64、sqrtd128
- 相关操作 cbrt(类型通用宏)、hypot(类型通用宏)、pow(类型通用宏)、rootn(类型通用宏)、rsqrt(类型通用宏)