La funzione hypot () in C ++ restituisce la radice quadrata della somma dei quadrati degli argomenti passati.
prototipo di hypot ()
doppia ipotesi (doppia x, doppia y); float hypot (float x, float y); ipotesi doppia lunga (doppia x lunga, doppia y lunga); Pow promosso (Type1 x, Type2 y); doppia ipotesi (doppia x, doppia y, doppia x); // (da C ++ 17) float hypot (float x, float y, float z); // (dal C ++ 17) ipotesi doppia lunga (doppia x lunga, doppia y lunga, doppia z lunga); // (da C ++ 17) Promosso pow (Type1 x, Type2 y, Type2 y); // (dal C ++ 17)
Poiché C ++ 11, se un argomento passato a hypot () è long double
, il tipo restituito Promoted è long double
. In caso contrario, il tipo restituito Promosso è double
.
h = √ (x2 + y2
in matematica è equivalente a
h = ipotesi (x, y);
nella programmazione C ++.
Se vengono passati tre argomenti:
h = √ (x2 + y2 + z2))
in matematica è equivalente a
h = ipotesi (x, y);
nella programmazione C ++.
Questa funzione è definita nel file di intestazione.
parametri hypot ()
Hytpot () accetta 2 o 3 parametri di tipo integrale o in virgola mobile.
hypot () Valore restituito
L'ipotesi () restituisce:
- l'ipotenusa di un triangolo rettangolo se vengono passati due argomenti, es .
√(x2+y2)
- distanza dall'origine a (x, y, x) se vengono passati tre argomenti, cioè .
√(x2+y2+z2)
Esempio 1: come funziona hypot () in C ++?
#include #include using namespace std; int main() ( double x = 2.1, y = 3.1, result; result = hypot(x, y); cout << "hypot(x, y) = " << result << endl; long double yLD, resultLD; x = 3.52; yLD = 5.232342323; // hypot() returns long double in this case resultLD = hypot(x, yLD); cout << "hypot(x, yLD) = " << resultLD; return 0; )
Quando esegui il programma, l'output sarà:
ipotesi (x, y) = 3,74433 ipotesi (x, yLD) = 6,30617
Esempio 2: hypot () con tre argomenti
#include #include using namespace std; int main() ( double x = 2.1, y = 3.1, z = 23.3, result; result = hypot(x, y, z); cout << "hypot(x, y, z) = " << result << endl; return 0; )
Nota: questo programma verrà eseguito solo nei nuovi compilatori che supportano C ++ 17.