Funktionen hypot () i C ++ returnerar kvadratroten av summan av kvadraten av skickade argument.
hypot () prototyp
dubbel hypot (dubbel x, dubbel y); float hypot (float x, float y); lång dubbel hypot (lång dubbel x, lång dubbel y); Främjad drivkraft (Type1 x, Type2 y); dubbel hypot (dubbel x, dubbel y, dubbel x); // (sedan C ++ 17) float hypot (float x, float y, float z); // (sedan C ++ 17) lång dubbel hypot (lång dubbel x, lång dubbel y, lång dubbel z); // (sedan C ++ 17) Promoted pow (Type1 x, Type2 y, Type2 y); // (sedan C ++ 17)
Eftersom C ++ 11, om något argument skickat till hypot () är long double
, är returtypen Promoted long double
. Om inte är returtypen Promoted double
.
h = √ (x2 + y2
i matematik motsvarar
h = hypot (x, y);
i C ++ programmering.
Om tre argument godkänns:
h = √ (x2 + y2 + z2))
i matematik motsvarar
h = hypot (x, y);
i C ++ programmering.
Denna funktion definieras i rubrikfilen.
hypot () Parametrar
Hytpot () tar antingen 2 eller 3 parametrar av integral- eller flytpunktstyp.
hypot () Returvärde
Hypoten () returnerar:
- hypotenusan av en rätvinklig triangel om två argument passerat, det vill säga .
√(x2+y2)
- avståndet från origo till till (x, y, x) om tre argument skickas, dvs .
√(x2+y2+z2)
Exempel 1: Hur fungerar hypot () i 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; )
När du kör programmet blir resultatet:
hypot (x, y) = 3,74433 hypot (x, yLD) = 6,30617
Exempel 2: hypot () med tre argument
#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; )
Obs! Det här programmet körs bara i nya kompilatorer som stöder C ++ 17.