Namespaces
Variants

std:: hypot, std:: hypotf, std:: hypotl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
헤더 파일에 정의됨 <cmath>
(1)
float hypot ( float x, float y ) ;

double hypot ( double x, double y ) ;

long double hypot ( long double x, long double y ) ;
(C++11부터)
(C++23까지)
/*floating-point-type*/

hypot ( /*floating-point-type*/ x,

/*floating-point-type*/ y ) ;
(C++23부터)
(constexpr since C++26)
float hypotf ( float x, float y ) ;
(2) (C++11부터)
(constexpr since C++26)
long double hypotl ( long double x, long double y ) ;
(3) (C++11부터)
(constexpr since C++26)
(4)
float hypot ( float x, float y, float z ) ;

double hypot ( double x, double y, double z ) ;

long double hypot ( long double x, long double y, long double z ) ;
(C++17부터)
(C++23까지)
/*floating-point-type*/

hypot ( /*floating-point-type*/ x,
/*floating-point-type*/ y,

/*floating-point-type*/ z ) ;
(C++23부터)
(constexpr since C++26)
헤더 파일에 정의됨 <cmath>
template < class Arithmetic1, Arithmetic2 >

/*common-floating-point-type*/

hypot ( Arithmetic1 x, Arithmetic2 y ) ;
(A) (C++11부터)
(constexpr since C++26)
template < class Arithmetic1, Arithmetic2, Arithmetic3 >

/*common-floating-point-type*/

hypot ( Arithmetic1 x, Arithmetic2 y, Arithmetic3 z ) ;
(B) (C++17부터)
1-3) x y 의 제곱합의 제곱근을 계산하며, 계산 중간 단계에서 과도한 오버플로우 또는 언더플로우가 발생하지 않도록 합니다. 라이브러리는 모든 cv-unqualified 부동 소수점 타입에 대해 매개변수 x y 의 타입으로 std::hypot 의 오버로드를 제공합니다. (C++23부터)
4) x , y , 그리고 z 의 제곱합의 제곱근을 계산하며, 계산의 중간 단계에서 과도한 오버플로우나 언더플로우가 발생하지 않도록 합니다. 라이브러리는 모든 cv-unqualified 부동 소수점 타입에 대해 매개변수 x , y , z 의 타입으로 std::hypot 의 오버로드를 제공합니다. (C++23부터)
A,B) 다른 모든 산술 타입 조합에 대해 추가 오버로드가 제공됩니다.

이 함수의 두 인자 버전으로 계산되는 값은 길이가 x y 인 직각삼각형의 빗변 길이, 또는 원점 (0,0) 에서 점 (x,y) 까지의 거리, 또는 복소수 x+ i y 의 크기입니다.

이 함수의 세 인자 버전으로 계산된 값은 점 (x,y,z) 와 원점 (0,0,0) 사이의 거리입니다.

목차

매개변수

x, y, z - 부동 소수점 또는 정수 값

반환값

1-3,A) 오류가 발생하지 않으면 직각 삼각형의 빗변, x 2
+y 2
이 반환됩니다.
4,B) 오류가 발생하지 않으면, 3D 공간에서 원점으로부터의 거리, x 2
+y 2
+z 2
가 반환됩니다.

오버플로로 인한 범위 오류가 발생하면, +HUGE_VAL , +HUGE_VALF , 또는 +HUGE_VALL 가 반환됩니다.

언더플로로 인한 범위 오류가 발생하면, 올바른 결과(반올림 후)가 반환됩니다.

오류 처리

오류는 math_errhandling 에 명시된 대로 보고됩니다.

구현이 IEEE 부동 소수점 연산(IEC 60559)을 지원하는 경우,

  • std :: hypot ( x, y ) , std :: hypot ( y, x ) , 그리고 std :: hypot ( x, - y ) 는 동등합니다.
  • 인수 중 하나가 ±0인 경우, std :: hypot ( x, y ) 는 0이 아닌 인수로 호출된 std::fabs 와 동등합니다.
  • 인수 중 하나가 ±∞인 경우, std :: hypot ( x, y ) 는 다른 인수가 NaN인 경우에도 +∞를 반환합니다.
  • 그렇지 않으면, 인수 중 하나라도 NaN인 경우 NaN이 반환됩니다.

참고 사항

구현체들은 일반적으로 1 ulp (Unit in the Last Place — 최소 정밀도 단위) 미만의 정밀도를 보장합니다: GNU , BSD .

std :: hypot ( x, y ) std :: abs ( std:: complex < double > ( x, y ) ) 와 동등합니다.

POSIX는 언더플로우가 두 인수가 모두 서브노멀(subnormal)이고 올바른 결과 또한 서브노멀인 경우에만 발생할 수 있도록 명시합니다(이는 단순한 구현을 금지합니다).

3차원 공간에서 두 점 (x1, y1, z1) (x2, y2, z2) 사이의 거리는 3인자 오버로드 std::hypot 을 사용하여 std :: hypot ( x2 - x1, y2 - y1, z2 - z1 ) 로 계산할 수 있습니다.

(C++17부터)

추가 오버로드는 반드시 (A,B) 형태로 정확히 제공될 필요가 없습니다. 첫 번째 인수 num1 , 두 번째 인수 num2 , 그리고 선택적 세 번째 인수 num3 에 대해 다음을 보장할 수 있을 만큼 충분하기만 하면 됩니다:

  • 만약 num1 , num2 또는 num3 long double 타입을 가지면,
  • std :: hypot ( num1, num2 ) 는 다음과 동일한 효과를 가집니다: std :: hypot ( static_cast < long double > ( num1 ) ,
    static_cast < long double > ( num2 ) )
    , 그리고
  • std :: hypot ( num1, num2, num3 ) 는 다음과 동일한 효과를 가집니다: std :: hypot ( static_cast < long double > ( num1 ) ,
    static_cast < long double > ( num2 ) ,
    static_cast < long double > ( num3 ) )
    .
  • 그렇지 않고, num1 , num2 및/또는 num3 double 타입이거나 정수 타입인 경우,
  • std :: hypot ( num1, num2 ) 는 다음과 동일한 효과를 가집니다: std :: hypot ( static_cast < double > ( num1 ) ,
    static_cast < double > ( num2 ) )
    , 그리고
  • std :: hypot ( num1, num2, num3 ) 는 다음과 동일한 효과를 가집니다: std :: hypot ( static_cast < double > ( num1 ) ,
    static_cast < double > ( num2 ) ,
    static_cast < double > ( num3 ) )
    .
  • 그렇지 않고, num1 , num2 또는 num3 float 타입을 가지면,
  • std :: hypot ( num1, num2 ) 는 다음 코드와 동일한 효과를 가집니다: std :: hypot ( static_cast < float > ( num1 ) ,
    static_cast < float > ( num2 ) )
    , 그리고
  • std :: hypot ( num1, num2, num3 ) 는 다음 코드와 동일한 효과를 가집니다: std :: hypot ( static_cast < float > ( num1 ) ,
    static_cast < float > ( num2 ) ,
    static_cast < float > ( num3 ) )
    .
(C++23 이전)

만약 num1 , num2 그리고 num3 이 산술 타입을 가지면,

  • std :: hypot ( num1, num2 ) 는 다음 코드와 동일한 효과를 가집니다: std :: hypot ( static_cast < /*common-floating-point-type*/ > ( num1 ) ,
    static_cast < /*common-floating-point-type*/ > ( num2 ) )
    , 그리고
  • std :: hypot ( num1, num2, num3 ) 는 다음 코드와 동일한 효과를 가집니다: std :: hypot ( static_cast < /*common-floating-point-type*/ > ( num1 ) ,
    static_cast < /*common-floating-point-type*/ > ( num2 ) ,
    static_cast < /*common-floating-point-type*/ > ( num3 ) )
    ,

여기서 /*common-floating-point-type*/ num1 , num2 그리고 num3 의 타입들 중 가장 높은 부동소수점 변환 등급 과 가장 높은 부동소수점 변환 하위 등급 을 가지는 부동소수점 타입이며, 정수 타입의 인수들은 double 과 동일한 부동소수점 변환 등급을 가진 것으로 간주됩니다.

만약 가장 높은 등급과 하위 등급을 가진 부동소수점 타입이 존재하지 않으면, 오버로드 해결 은 제공된 오버로드들 중에서 사용 가능한 후보를 결과로 내지 않습니다.

(C++23부터)
기능 테스트 매크로 표준 기능
__cpp_lib_hypot 201603L (C++17) std::hypot 의 3-인자 오버로드 (4,B)

예제

#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
struct Point3D { float x, y, z; };
int main()
{
    // 일반적인 사용법
    std::cout << "(1,1) cartesian is (" << std::hypot(1, 1)
              << ',' << std::atan2(1,1) << ") polar\n";
    Point3D a{3.14, 2.71, 9.87}, b{1.14, 5.71, 3.87};
    // C++17은 3인자 hypot 오버로드를 가짐:
    std::cout << "distance(a,b) = "
              << std::hypot(a.x - b.x, a.y - b.y, a.z - b.z) << '\n';
    // 특수 값
    std::cout << "hypot(NAN,INFINITY) = " << std::hypot(NAN, INFINITY) << '\n';
    // 오류 처리
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "hypot(DBL_MAX,DBL_MAX) = " << std::hypot(DBL_MAX, DBL_MAX) << '\n';
    if (errno == ERANGE)
        std::cout << "    errno = ERANGE " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_OVERFLOW))
        std::cout << "    FE_OVERFLOW raised\n";
}

출력:

(1,1) cartesian is (1.41421,0.785398) polar
distance(a,b) = 7
hypot(NAN,INFINITY) = inf
hypot(DBL_MAX,DBL_MAX) = inf
    errno = ERANGE Numerical result out of range
    FE_OVERFLOW raised

참고 항목

(C++11) (C++11)
주어진 거듭제곱으로 수를 올림 ( x y )
(함수)
(C++11) (C++11)
제곱근을 계산함 ( x )
(함수)
(C++11) (C++11) (C++11)
세제곱근을 계산함 ( 3 x )
(함수)
복소수의 크기를 반환함
(함수 템플릿)