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++ ではオーバーロードが可能であるため、sqrt または float 型を受け取る 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

関連項目

数学と浮動小数点のサポート
exp, expf, expl
log, logf, log10, log10f
pow, powf, powl
_CIsqrt