Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / C

hypot — função matemática C23

`hypot(x, y)` de `<math.h>` devolve o `sqrt(x*x + y*y)` não negativo, calculando-o sem overflow ou underflow intermédio indevido. `hypotf` e `hypotl` servem respetivamente `float` e `long double`.

Versão: C23

Sintaxe

double hypot(double x, double y);

hypot(x, y) de <math.h> devolve o sqrt(x*x + y*y) não negativo, calculando-o sem overflow ou underflow intermédio indevido. hypotf e hypotl servem respetivamente float e long double.

O exemplo imprime 5.0 para lados 3 e 4 e depois avalia hypot(1e308, 1e308). O resultado fica finito e superior a 1e308; 5.0 1 1 demonstra a vantagem sobre sqrt(x*x + y*y), cujos quadrados fariam overflow.

Se o resultado final ficar fora do intervalo pode ocorrer range error; consulta math_errhandling, errno ou exceções floating-point. Sob Annex F, argumento infinito dá +Infinity mesmo se o outro for NaN; NaN sem infinito propaga-se. Dois zeros dão +0 sem sinal negativo.

Exemplos

#include <math.h>
#include <stdio.h>
int main(void){double ordinary=hypot(3.0,4.0),large=hypot(1e308,1e308);printf("%.1f %d %d\n",ordinary,isfinite(large)!=0,large>1e308);return 0;}

Resultado

5.0 1 1

O exemplo imprime `5.0` para lados 3 e 4 e depois avalia `hypot(1e308, 1e308)`. O resultado fica finito e superior a `1e308`; `5.0 1 1` demonstra a vantagem sobre `sqrt(x*x + y*y)`, cujos quadrados fariam overflow.

Fontes