C ++ hypot () - C ++ standaardbibliotheek

De functie hypot () in C ++ retourneert de vierkantswortel van de som van het kwadraat van doorgegeven argumenten.

hypot () prototype

dubbele hypot (dubbele x, dubbele y); float hypot (float x, float y); lange dubbele hypot (lange dubbele x, lange dubbele y); Gepromoveerde pow (Type1 x, Type2 y); dubbele hypot (dubbele x, dubbele y, dubbele x); // (sinds C ++ 17) float hypot (float x, float y, float z); // (sinds C ++ 17) lange dubbele hypot (lange dubbele x, lange dubbele y, lange dubbele z); // (sinds C ++ 17) Gepromoveerde pow (Type1 x, Type2 y, Type2 y); // (sinds C ++ 17)

Sinds C ++ 11, als een argument is doorgegeven aan hypot (), is long doublehet retourtype Promoted long double. Zo niet, dan is het retourtype Gepromoot double.

 h = √ (x2 + y2

in de wiskunde is gelijk aan

 h = hypot (x, y);

in C ++ programmeren.

Als er drie argumenten worden doorgegeven:

 h = √ (x2 + y2 + z2))

in de wiskunde is gelijk aan

 h = hypot (x, y);

in C ++ programmeren.

Deze functie is gedefinieerd in het header-bestand.

hypot () Parameters

De hytpot () accepteert 2 of 3 parameters van het type integraal of drijvende komma.

hypot () Retourwaarde

De hypot () geeft als resultaat:

  • de hypotenusa van een rechthoekige driehoek als twee argumenten worden doorgegeven, dwz .√(x2+y2)
  • afstand van de oorsprong tot de (x, y, x) als er drie argumenten worden doorgegeven, dwz .√(x2+y2+z2)

Voorbeeld 1: Hoe werkt 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; ) 

Wanneer u het programma uitvoert, is de uitvoer:

 hypot (x, y) = 3,74433 hypot (x, yLD) = 6,30617 

Voorbeeld 2: hypot () met drie 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; )

Opmerking: dit programma kan alleen worden uitgevoerd in nieuwe compilers die C ++ 17 ondersteunen.

Interessante artikelen...