Die Funktion hypot () in C ++ gibt die Quadratwurzel der Summe der übergebenen Argumente zurück.
hypot () Prototyp
doppeltes Hypot (doppeltes x, doppeltes y); float hypot (float x, float y); langes doppeltes Hypot (langes doppeltes x, langes doppeltes y); Geförderte Leistung (Typ 1 x, Typ 2 y); doppeltes Hypot (doppeltes x, doppeltes y, doppeltes x); // (seit C ++ 17) float hypot (float x, float y, float z); // (seit C ++ 17) long double hypot (langes doppeltes x, langes doppeltes y, langes doppeltes z); // (seit C ++ 17) Promoted pow (Typ1 x, Typ2 y, Typ2 y); // (seit C ++ 17)
Seit C ++ 11 lautet long double
der zurückgegebene Typ Promoted , wenn ein an hypot () übergebenes Argument lautet long double
. Wenn nicht, lautet der zurückgegebene Rückgabetyp double
.
h = √ (x2 + y2
in der Mathematik ist gleichbedeutend mit
h = Hypot (x, y);
in der C ++ - Programmierung.
Wenn drei Argumente übergeben werden:
h = √ (x2 + y2 + z2))
in der Mathematik ist gleichbedeutend mit
h = Hypot (x, y);
in der C ++ - Programmierung.
Diese Funktion ist in der Header-Datei definiert.
hypot () Parameter
Der hytpot () akzeptiert entweder 2 oder 3 Parameter vom Integral- oder Gleitkomma-Typ.
hypot () Rückgabewert
Das hypot () gibt zurück:
- die Hypotenuse eines rechtwinkligen Dreiecks, wenn zwei Argumente übergeben werden, dh .
√(x2+y2)
- Abstand vom Ursprung zum (x, y, x), wenn drei Argumente übergeben werden, dh , .
√(x2+y2+z2)
Beispiel 1: Wie funktioniert 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; )
Wenn Sie das Programm ausführen, lautet die Ausgabe wie folgt:
Hypot (x, y) = 3,74433 Hypot (x, yLD) = 6,30617
Beispiel 2: hypot () mit drei Argumenten
#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; )
Hinweis: Dieses Programm wird nur in neuen Compilern ausgeführt, die C ++ 17 unterstützen.