Compartilhar via


sqrt, sqrtf, sqrtl

Calcula a raiz quadrada.

Sintaxe

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

Parâmetros

x
Um valor de ponto flutuante não negativo

Comentários

Como C++ permite a sobrecarga, você pode chamar sobrecargas de sqrt que usam o tipo float ou long double. Em um programa do C, a menos que você esteja usando a macro <tgmath.h> para chamar essa função, sqrt sempre recebe e retorna um double.

Se você usar a macro <tgmath.h> sqrt(), o tipo do argumento determinará qual versão da função será selecionada. Confira Matemática do tipo genérico para obter detalhes.

Por padrão, o estado global dessa função tem como escopo o aplicativo. Para alterar esse comportamento, consulte Estado global na CRT.

Retornar valor

A função sqrt retorna a raiz quadrada de x. Por padrão, se x for um valor negativo, sqrt retornará um NaN indefinido.

Entrada Exceção SEH Exceção _matherr
± QNaN, IND nenhum _DOMAIN
-INF nenhum _DOMAIN
x < 0 nenhum _DOMAIN

Requisitos

Função Cabeçalho C Cabeçalho C++
sqrt, sqrtf, sqrtl <math.h> <cmath>
Macro sqrt <tgmath.h>

Para obter informações sobre compatibilidade, consulte Compatibilidade.

Exemplo

// 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

Confira também

Suporte a matemática e ponto flutuante
exp, expf, expl
log, logf, log10, log10f
pow, powf, powl
_CIsqrt