sqrt() — 平方根の計算

形式

#include <math.h>
double sqrt(double x);

言語レベル

ANSI

スレッド・セーフ

はい

説明

sqrt() 関数は、 xの平方根の負でない値を計算します。

戻り値

sqrt() 関数は、平方根の結果を返します。 x が負の場合、関数は errnoEDOM に設定し、0 を戻します。

この例では、main の最初の引数として渡される数量の平方根を計算します。 この関数は、ユーザーが負の値を渡すとエラー・メッセージを出力します。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main(int argc, char ** argv)
{
  char * rest;
  double value;
 
  if ( argc != 2 )
    printf( "Usage: %s value\n", argv[0] );
  else
  {
    value = strtod( argv[1], &rest);
    if ( value < 0.0 )
       printf( "sqrt of a negative number\n" );
    else
       printf("sqrt( %lf ) = %lf\n", value, sqrt( value ));
  }
}
 
/********************  If the input is 45,  *****************************
****************  then the output should be similar to:  **********
 
sqrt( 45.000000 ) = 6.708204
*/

関連情報