---
title: sqrt
description: "计算x的平方根。"
url: https://www.hikunpeng.com/document/detail/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_0202.html
sourcePath: /source/zh/kunpenghpcs/hpckit/devg/kunpengaccel_kml_0202.html
indexId: 6c71c0f8fdf025626e87ba0d2914818f344556fb595e59a0a863721e3404d9b761
---
# sqrt

计算x的平方根。

#### 接口定义

C interface：

float sqrtf(float x);

double sqrt(double x);

long double sqrtl(long double x);

Fortran interface：

RES = SQRTF(X);

RES = SQRT(X);


#### 参数

| 参数名 | 类型 | 描述 | 输入/输出 |
| --- | --- | --- | --- |
| x | 在sqrtf中，x是单精度浮点类型。 在sqrt中，x是双精度浮点类型。 在sqrtl中，x是长双精度浮点类型。 | 表示输入数据的浮点值。 | 输入 |


#### 返回值

- 输入x为±0，返回±0。
- 输入x为+∞，返回+∞。
- 输入x为-∞，返回nan。
- 输入x为nan，返回nan。


#### 依赖

C: "km.h"


#### 示例

C interface：

```
// typical usage 
    double x1 = 1.0, x2 = 2.0, x3 = 3.0, x4 = 4.0; 
    // special handling 
    double a = 0.0, b = INFINITY, c = -INFINITY, d = NAN; 
    // print result 
        printf("sqrt(1.0) = %.15f\n", sqrt(x1)); 
        printf("sqrt(2.0) = %.15f\n", sqrt(x2)); 
        printf("sqrt(3.0) = %.15f\n", sqrt(x3)); 
        printf("sqrt(4.0) = %.15f\n", sqrt(x4)); 
        printf("sqrt(0.0) = %.15f\n", sqrt(a)); 
        printf("sqrt(INFINITY) = %.15f\n", sqrt(b)); 
        printf("sqrt(-INFINITY) = %.15f\n", sqrt(c)); 
        printf("sqrt(NAN) = %.15f\n", sqrt(d)); 
    /* 
     *  sqrt(1.0) = 1.000000000000000 
     *  sqrt(2.0) = 1.414213562373095 
     *  sqrt(3.0) = 1.732050807568877 
     *  sqrt(4.0) = 2.000000000000000 
     *  sqrt(0.0) = 0.000000000000000 
     *  sqrt(INFINITY) = inf 
     *  sqrt(-INFINITY) = -inf
     *  sqrt(NAN) = nan 
     * */
```

Fortran interface：

```
REAL(8) :: X = 3.0
PRINT*,  SQRT(X)
!
! OUTPUT
!     1.732050807568877
!
```
