sqrt ()- 計算平方根

格式

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

語言層次

ANSI

安全執行緒

說明

sqrt() 函數會計算 x平方根的非負值。

回覆值

sqrt() 函數會傳回平方根結果。 如果 x 是負數,則函數會將 errno 設為 EDOM,並傳回 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
*/

相關資訊