Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/math/hypot"

From cppreference.com
< cpp‎ | numeric‎ | math
m (Undo revision 108836 by 2600:1700:2320:3440:1C49:AB7A:1AFE:E04B (talk))
m (., http -> https)
 
(16 intermediate revisions by 7 users not shown)
Line 2: Line 2:
 
{{cpp/numeric/math/navbar}}
 
{{cpp/numeric/math/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | cmath}}
+
{{dcl header|cmath}}
{{dcl | since=c++11 |num=1|
+
{{dcl rev multi|num=1|since1=c++11|dcl1=
 
float      hypot ( float x, float y );
 
float      hypot ( float x, float y );
float      hypotf( float x, float y );
 
}}
 
{{dcl | since=c++11 |num=2|
 
 
double      hypot ( double x, double y );
 
double      hypot ( double x, double y );
}}
 
{{dcl | since=c++11 |num=3|
 
 
long double hypot ( long double x, long double y );
 
long double hypot ( long double x, long double y );
long double hypotl( long double x, long double y );
+
|since2=c++23|dcl2=
 +
/* floating-point-type */
 +
            hypot ( /* floating-point-type */ x,
 +
                    /* floating-point-type */ y );
 +
|notes2={{mark|constexpr since C++26}}
 
}}
 
}}
{{dcl | since=c++11 | num=4|
+
{{dcl|num=2|since=c++11|notes={{mark|constexpr since C++26}}|
Promoted    hypot ( Arithmetic1 x, Arithmetic2 y );
+
float      hypotf( float x, float y );
 
}}
 
}}
{{dcl | since=c++17 |num=5|
+
{{dcl|num=3|since=c++11|notes={{mark|constexpr since C++26}}|
float      hypot ( float x, float y, float z );
+
long double hypotl( long double x, long double y );
 
}}
 
}}
{{dcl | since=c++17 |num=6|
+
{{dcl rev multi|num=4|since1=c++17|dcl1=
 +
float      hypot ( float x, float y, float z );
 
double      hypot ( double x, double y, double z );
 
double      hypot ( double x, double y, double z );
}}
 
{{dcl | since=c++17 |num=7|
 
 
long double hypot ( long double x, long double y, long double z );
 
long double hypot ( long double x, long double y, long double z );
 +
|since2=c++23|dcl2=
 +
/* floating-point-type */
 +
            hypot ( /* floating-point-type */ x,
 +
                    /* floating-point-type */ y,
 +
                    /* floating-point-type */ z );
 +
|notes2={{mark|constexpr since C++26}}
 
}}
 
}}
{{dcl | since=c++17 | num=8|
+
{{dcl h|[[#Notes|Additional overloads]]}}
Promoted    hypot ( Arithmetic1 x, Arithmetic2 y, Arithmetic3 z );
+
{{dcl header|cmath}}
 +
{{dcl|num=A|since=c++11|notes={{mark|constexpr since C++26}}|
 +
template< class Arithmetic1, Arithmetic2 >
 +
/* common-floating-point-type */
 +
            hypot ( Arithmetic1 x, Arithmetic2 y );
 +
}}
 +
{{dcl|num=B|since=c++17|notes={{mark|constexpr since C++26}}|
 +
template< class Arithmetic1, Arithmetic2, Arithmetic3 >
 +
/* common-floating-point-type */
 +
            hypot ( Arithmetic1 x, Arithmetic2 y, Arithmetic3 z );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1-3@ Computes the square root of the sum of the squares of {{tt|x}} and {{tt|y}}, without undue overflow or underflow at intermediate stages of the computation.
+
@1-3@ Computes the square root of the sum of the squares of {{c|x}} and {{c|y}}, without undue overflow or underflow at intermediate stages of the computation.{{rev inl|since=c++23|The library provides overloads of {{tt|std::hypot}} for all cv-unqualified floating-point types as the type of the parameters {{c|x}} and {{c|y}}.}}
@4@ A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by {{v|1-3}}. If any argument has [[cpp/types/is_integral|integral type]], it is cast to {{c|double}}. If any other argument is {{c|long double}}, then the return type is {{c|long double}}, otherwise it is {{c|double}}.
+
@4@ Computes the square root of the sum of the squares of {{c|x}}, {{c|y}}, and {{c|z}}, without undue overflow or underflow at intermediate stages of the computation.{{rev inl|since=c++23|The library provides overloads of {{tt|std::hypot}} for all cv-unqualified floating-point types as the type of the parameters {{c|x}}, {{c|y}} and {{c|z}}.}}
@5-7@ Computes the square root of the sum of the squares of {{tt|x}}, {{tt|y}}, and {{tt|z}}, without undue overflow or underflow at intermediate stages of the computation.
+
@A,B@ Additional overloads are provided for all other combinations of arithmetic types.
@8@ A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by {{v|5-7}}. If any argument has [[cpp/types/is_integral|integral type]], it is cast to {{c|double}}. If any other argument is {{c|long double}}, then the return type is {{c|long double}}, otherwise it is {{c|double}}.
+
  
The value computed by the two-argument version of this function is the length of the hypotenuse of a right-angled triangle with sides of length {{tt|x}} and {{tt|y}}, or the distance of the point {{tt|(x,y)}} from the origin {{tt|(0,0)}}, or the magnitude of a complex number {{tt|x+''i''y}}
+
The value computed by the two-argument version of this function is the length of the hypotenuse of a right-angled triangle with sides of length {{c|x}} and {{c|y}}, or the distance of the point {{tt|(x,y)}} from the origin {{tt|(0,0)}}, or the magnitude of a complex number {{tt|x+''i''y}}.
  
 
The value computed by the three-argument version of this function is the distance of the point {{tt|(x,y,z)}} from the origin {{tt|(0,0,0)}}.
 
The value computed by the three-argument version of this function is the distance of the point {{tt|(x,y,z)}} from the origin {{tt|(0,0,0)}}.
Line 42: Line 54:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | x, y, z | values of floating-point or [[cpp/types/is_integral|integral types]]}}
+
{{par|x, y, z|floating-point or integer values}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
@1-4@ If no errors occur, the hypotenuse of a right-angled triangle, {{math|{{mrad|x{{su|p=2}}+y{{su|p=2}}}}}}, is returned.
+
@1-3,A@ If no errors occur, the hypotenuse of a right-angled triangle, {{mathjax-or|1=\(\scriptsize{\sqrt{x^2+y^2} }\)|2={{mrad|x{{su|p=2}}+y{{su|p=2}}}}}}, is returned.
@5-8@ If no errors occur, the distance from origin in 3D space, {{math|{{mrad|x{{su|p=2}}+y{{su|p=2}}+z{{su|p=2}}}}}}, is returned.
+
@4,B@ If no errors occur, the distance from origin in 3D space, {{mathjax-or|1=\(\scriptsize{\sqrt{x^2+y^2+z^2} }\)|2={{mrad|x{{su|p=2}}+y{{su|p=2}}+z{{su|p=2}}}}}}, is returned.
  
If a range error due to overflow occurs, {{tt|+HUGE_VAL}}, {{tt|+HUGE_VALF}}, or {{tt|+HUGE_VALL}} is returned.
+
If a range error due to overflow occurs, {{lc|HUGE_VAL|+HUGE_VAL}}, {{tt|+HUGE_VALF}}, or {{tt|+HUGE_VALL}} is returned.
  
 
If a range error due to underflow occurs, the correct result (after rounding) is returned.
 
If a range error due to underflow occurs, the correct result (after rounding) is returned.
  
 
===Error handling===
 
===Error handling===
Errors are reported as specified in {{lc|math_errhandling}}
+
Errors are reported as specified in {{lc|math_errhandling}}.
  
 
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
 
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
* {{c|hypot(x, y)}}, {{c|hypot(y, x)}}, and {{c|hypot(x, -y)}} are equivalent
+
* {{c|std::hypot(x, y)}}, {{c|std::hypot(y, x)}}, and {{c|std::hypot(x, -y)}} are equivalent.
* if one of the arguments is ±0, {{tt|hypot(x,y)}} is equivalent to {{c|fabs}} called with the non-zero argument
+
* if one of the arguments is ±0, {{c|std::hypot(x, y)}} is equivalent to {{lc|std::fabs}} called with the non-zero argument.
* if one of the arguments is ±∞, {{tt|hypot(x,y)}} returns +∞ even if the other argument is NaN
+
* if one of the arguments is ±∞, {{c|std::hypot(x, y)}} returns +∞ even if the other argument is NaN.
* otherwise, if any of the arguments is NaN, NaN is returned
+
* otherwise, if any of the arguments is NaN, NaN is returned.
  
 
===Notes===
 
===Notes===
Implementations usually guarantee precision of less than 1 ulp (units in the last place): [http://sourceware.org/git/?p=glibc.git;a=blob_plain;f=sysdeps/ieee754/dbl-64/e_hypot.c GNU], [http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/msun/src/e_hypot.c?rev=1.13.4.2;content-type=text%2Fplain BSD], [http://www.open64.net/doc/d5/d3f/hypot_8c-source.html Open64]
+
Implementations usually guarantee precision of less than 1 {{enwiki|Unit in the last place|ulp}} (Unit in the Last Place &mdash; Unit of Least Precision): [https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=sysdeps/ieee754/dbl-64/e_hypot.c GNU], [https://www.freebsd.org/cgi/cvsweb.cgi/src/lib/msun/src/e_hypot.c BSD].
  
{{c|std::hypot(x, y)}} is equivalent to {{c|std::abs(std::complex<double>(x,y))}}
+
{{c|std::hypot(x, y)}} is equivalent to {{c|std::abs(std::complex<double>(x, y))}}.
  
[http://pubs.opengroup.org/onlinepubs/9699919799/functions/hypot.html POSIX specifies] that underflow may only occur when both arguments are subnormal and the correct result is also subnormal (this forbids naive implementations)
+
[https://pubs.opengroup.org/onlinepubs/9699919799/functions/hypot.html POSIX specifies] that underflow may only occur when both arguments are subnormal and the correct result is also subnormal (this forbids naive implementations).
  
Distance between two points {{tt|(x1,y1,z1)}} and {{tt|(x2,y2,z2)}}on 3D space can be calculated as {{c|std::hypot(x2-x1, y2-y1, z2-z1)}}
+
{{rrev|since=c++17|
 +
Distance between two points {{tt|(x1,y1,z1)}} and {{tt|(x2,y2,z2)}} on 3D space can be calculated using 3-argument overload of {{tt|std::hypot}} as {{c|std::hypot(x2 - x1, y2 - y1, z2 - z1)}}.
 +
}}
 +
 
 +
{{cpp/numeric/math/additional overload note|hypot}}
 +
 
 +
{{feature test macro|__cpp_lib_hypot|std=C++17|value=201603L|3-argument overload of {{ttt|std::hypot}}}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <iostream>
+
#include <cmath>
+
 
#include <cerrno>
 
#include <cerrno>
 
#include <cfenv>
 
#include <cfenv>
 
#include <cfloat>
 
#include <cfloat>
 +
#include <cmath>
 
#include <cstring>
 
#include <cstring>
 +
#include <iostream>
 +
 +
// #pragma STDC FENV_ACCESS ON
 +
 +
struct Point3D { float x, y, z; };
  
#pragma STDC FENV_ACCESS ON
 
 
int main()
 
int main()
 
{
 
{
Line 88: Line 108:
 
     std::cout << "(1,1) cartesian is (" << std::hypot(1,1)
 
     std::cout << "(1,1) cartesian is (" << std::hypot(1,1)
 
               << ',' << std::atan2(1,1) << ") polar\n";
 
               << ',' << std::atan2(1,1) << ") polar\n";
 +
   
 +
    Point3D a{3.14, 2.71, 9.87}, b{1.14, 5.71, 3.87};
 +
    // C++17 has 3-argument hypot overload:
 +
    std::cout << "distance(a,b) = "
 +
              << std::hypot(a.x - b.x, a.y - b.y, a.z - b.z) << '\n';
 +
   
 
     // special values
 
     // special values
     std::cout << "hypot(NAN,INFINITY) = " << std::hypot(NAN,INFINITY) << '\n';
+
     std::cout << "hypot(NAN,INFINITY) = " << std::hypot(NAN, INFINITY) << '\n';
 +
   
 
     // error handling
 
     // error handling
 
     errno = 0;
 
     errno = 0;
 
     std::feclearexcept(FE_ALL_EXCEPT);
 
     std::feclearexcept(FE_ALL_EXCEPT);
     std::cout << "hypot(DBL_MAX,DBL_MAX) = " << std::hypot(DBL_MAX,DBL_MAX) << '\n';
+
     std::cout << "hypot(DBL_MAX,DBL_MAX) = " << std::hypot(DBL_MAX, DBL_MAX) << '\n';
 +
   
 
     if (errno == ERANGE)
 
     if (errno == ERANGE)
 
         std::cout << "    errno = ERANGE " << std::strerror(errno) << '\n';
 
         std::cout << "    errno = ERANGE " << std::strerror(errno) << '\n';
     if (fetestexcept(FE_OVERFLOW))
+
     if (std::fetestexcept(FE_OVERFLOW))
 
         std::cout << "    FE_OVERFLOW raised\n";
 
         std::cout << "    FE_OVERFLOW raised\n";
 
}
 
}
| output=
+
|output=
 
(1,1) cartesian is (1.41421,0.785398) polar
 
(1,1) cartesian is (1.41421,0.785398) polar
 +
distance(a,b) = 7
 
hypot(NAN,INFINITY) = inf
 
hypot(NAN,INFINITY) = inf
 
hypot(DBL_MAX,DBL_MAX) = inf
 
hypot(DBL_MAX,DBL_MAX) = inf
Line 109: Line 138:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/math/dsc pow}}
+
{{dsc inc|cpp/numeric/math/dsc pow}}
{{dsc inc | cpp/numeric/math/dsc sqrt}}
+
{{dsc inc|cpp/numeric/math/dsc sqrt}}
{{dsc inc | cpp/numeric/math/dsc cbrt}}
+
{{dsc inc|cpp/numeric/math/dsc cbrt}}
{{dsc inc | cpp/numeric/complex/dsc abs}}
+
{{dsc inc|cpp/numeric/complex/dsc abs}}
{{dsc see c | c/numeric/math/hypot}}
+
{{dsc see c|c/numeric/math/hypot}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 08:05, 14 September 2023

 
 
 
 
Defined in header <cmath>
(1)
float       hypot ( float x, float y );

double      hypot ( double x, double y );

long double hypot ( long double x, long double y );
(since C++11)
(until C++23)
/* floating-point-type */

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

                    /* floating-point-type */ y );
(since C++23)
(constexpr since C++26)
float       hypotf( float x, float y );
(2) (since C++11)
(constexpr since C++26)
long double hypotl( long double x, long double y );
(3) (since 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 );
(since C++17)
(until C++23)
/* floating-point-type */

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

                    /* floating-point-type */ z );
(since C++23)
(constexpr since C++26)
Defined in header <cmath>
template< class Arithmetic1, Arithmetic2 >

/* common-floating-point-type */

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

/* common-floating-point-type */

            hypot ( Arithmetic1 x, Arithmetic2 y, Arithmetic3 z );
(B) (since C++17)
(constexpr since C++26)
1-3) Computes the square root of the sum of the squares of x and y, without undue overflow or underflow at intermediate stages of the computation.The library provides overloads of std::hypot for all cv-unqualified floating-point types as the type of the parameters x and y.(since C++23)
4) Computes the square root of the sum of the squares of x, y, and z, without undue overflow or underflow at intermediate stages of the computation.The library provides overloads of std::hypot for all cv-unqualified floating-point types as the type of the parameters x, y and z.(since C++23)
A,B) Additional overloads are provided for all other combinations of arithmetic types.

The value computed by the two-argument version of this function is the length of the hypotenuse of a right-angled triangle with sides of length x and y, or the distance of the point (x,y) from the origin (0,0), or the magnitude of a complex number x+iy.

The value computed by the three-argument version of this function is the distance of the point (x,y,z) from the origin (0,0,0).

Contents

[edit] Parameters

x, y, z - floating-point or integer values

[edit] Return value

1-3,A) If no errors occur, the hypotenuse of a right-angled triangle, x2+y2, is returned.
4,B) If no errors occur, the distance from origin in 3D space, x2+y2+z2, is returned.

If a range error due to overflow occurs, +HUGE_VAL, +HUGE_VALF, or +HUGE_VALL is returned.

If a range error due to underflow occurs, the correct result (after rounding) is returned.

[edit] Error handling

Errors are reported as specified in math_errhandling.

If the implementation supports IEEE floating-point arithmetic (IEC 60559),

  • std::hypot(x, y), std::hypot(y, x), and std::hypot(x, -y) are equivalent.
  • if one of the arguments is ±0, std::hypot(x, y) is equivalent to std::fabs called with the non-zero argument.
  • if one of the arguments is ±∞, std::hypot(x, y) returns +∞ even if the other argument is NaN.
  • otherwise, if any of the arguments is NaN, NaN is returned.

[edit] Notes

Implementations usually guarantee precision of less than 1 ulp (Unit in the Last Place — Unit of Least Precision): GNU, BSD.

std::hypot(x, y) is equivalent to std::abs(std::complex<double>(x, y)).

POSIX specifies that underflow may only occur when both arguments are subnormal and the correct result is also subnormal (this forbids naive implementations).

Distance between two points (x1,y1,z1) and (x2,y2,z2) on 3D space can be calculated using 3-argument overload of std::hypot as std::hypot(x2 - x1, y2 - y1, z2 - z1).

(since C++17)

The additional overloads are not required to be provided exactly as (A,B). They only need to be sufficient to ensure that for their first argument num1, second argument num2 and the optional third argument num3:

  • If num1, num2 or num3 has type long double, then
  • std::hypot(num1, num2) has the same effect as std::hypot(static_cast<long double>(num1),
               static_cast<long double>(num2))
    , and
  • std::hypot(num1, num2, num3) has the same effect as std::hypot(static_cast<long double>(num1),
               static_cast<long double>(num2),
               static_cast<long double>(num3))
    .
  • Otherwise, if num1, num2 and/or num3 has type double or an integer type, then
  • std::hypot(num1, num2) has the same effect as std::hypot(static_cast<double>(num1),
               static_cast<double>(num2))
    , and
  • std::hypot(num1, num2, num3) has the same effect as std::hypot(static_cast<double>(num1),
               static_cast<double>(num2),
               static_cast<double>(num3))
    .
  • Otherwise, if num1, num2 or num3 has type float, then
  • std::hypot(num1, num2) has the same effect as std::hypot(static_cast<float>(num1),
               static_cast<float>(num2))
    , and
  • std::hypot(num1, num2, num3) has the same effect as std::hypot(static_cast<float>(num1),
               static_cast<float>(num2),
               static_cast<float>(num3))
    .
(until C++23)

If num1, num2 and num3 have arithmetic types, then

  • std::hypot(num1, num2) has the same effect as std::hypot(static_cast</* common-floating-point-type */>(num1),
               static_cast</* common-floating-point-type */>(num2))
    , and
  • std::hypot(num1, num2, num3) has the same effect as std::hypot(static_cast</* common-floating-point-type */>(num1),
               static_cast</* common-floating-point-type */>(num2),
               static_cast</* common-floating-point-type */>(num3))
    ,

where /* common-floating-point-type */ is the floating-point type with the greatest floating-point conversion rank and greatest floating-point conversion subrank among the types of num1, num2 and num3, arguments of integer type are considered to have the same floating-point conversion rank as double.

If no such floating-point type with the greatest rank and subrank exists, then overload resolution does not result in a usable candidate from the overloads provided.

(since C++23)
Feature-test macro Value Std Feature
__cpp_lib_hypot 201603L (C++17) 3-argument overload of std::hypot

[edit] Example

#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()
{
    // typical usage
    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 has 3-argument hypot overload:
    std::cout << "distance(a,b) = "
              << std::hypot(a.x - b.x, a.y - b.y, a.z - b.z) << '\n';
 
    // special values
    std::cout << "hypot(NAN,INFINITY) = " << std::hypot(NAN, INFINITY) << '\n';
 
    // error handling
    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";
}

Output:

(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

[edit] See also

(C++11)(C++11)
raises a number to the given power (xy)
(function) [edit]
(C++11)(C++11)
computes square root (x)
(function) [edit]
(C++11)(C++11)(C++11)
computes cube root (3x)
(function) [edit]
returns the magnitude of a complex number
(function template) [edit]
C documentation for hypot