Difference between revisions of "cpp/numeric/math/hypot"
m (+nan) |
(return values, explanation for 4), POSIX requirement on subnormals) |
||
Line 18: | Line 18: | ||
@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 {{tt|x}} and {{tt|y}}, without undue overflow or underflow at intermediate stages of the computation. | ||
− | @4@ 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@ A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by 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}}. |
The value computed by 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 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}} | ||
Line 28: | Line 28: | ||
===Return value=== | ===Return value=== | ||
− | + | If no errors occur, the hypotenuse of a right-angled triangle, {{math|{{mrad|x{{su|p=2}}+y{{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 underflow occurs, the correct result (after rounding) is returned. | ||
+ | |||
+ | ===Error handling=== | ||
Errors are reported as specified in {{lc|math_errhandling}} | Errors are reported as specified in {{lc|math_errhandling}} | ||
Line 44: | Line 48: | ||
{{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) | ||
===Example=== | ===Example=== | ||
{{example | {{example |
Revision as of 07:21, 22 May 2014
Defined in header <cmath>
|
||
float hypot( float x, float y ); |
(1) | (since C++11) |
double hypot( double x, double y ); |
(2) | (since C++11) |
long double hypot( long double x, long double y ); |
(3) | (since C++11) |
Promoted hypot( Arithmetic1 x, Arithmetic2 y ); |
(4) | (since C++11) |
x
and y
, without undue overflow or underflow at intermediate stages of the computation.The value computed by 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
Contents |
Parameters
x, y | - | values of floating-point or integral types |
Return value
If no errors occur, the hypotenuse of a right-angled triangle, √x2+y2, 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.
Error handling
Errors are reported as specified in math_errhandling
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
- hypot(x, y), hypot(y, x), and hypot(x, -y) are equivalent
- if one of the arguments is ±0,
hypot
is equivalent to fabs called with the non-zero argument - if one of the arguments is ±∞,
hypot
returns +∞ even if the other argument is NaN - otherwise, if any of the arguments is NaN, NaN is returned
Notes
Implementations usually guarantee precision of less than 1 ulp (units in the last place): GNU, BSD, Open64
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)
Example
#include <iostream> #include <cmath> #include <cerrno> #include <cfenv> #include <cfloat> #include <cstring> #pragma STDC FENV_ACCESS ON int main() { // typical usage std::cout << "(1,1) cartesian is (" << std::hypot(1,1) << ',' << std::atan2(1,1) << ") polar\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(fetestexcept(FE_OVERFLOW)) std::cout << " FE_OVERFLOW raised\n"; }
Output:
(1,1) cartesian is (1.41421,0.785398) polar hypot(NAN,INFINITY) = inf hypot(DBL_MAX,DBL_MAX) = inf errno = ERANGE Numerical result out of range FE_OVERFLOW raised
See also
(C++11)(C++11) |
computes square root (√x) (function) |
(C++11)(C++11) |
raises a number to the given power (xy) (function) |
returns the magnitude of a complex number (function template) | |
C documentation for hypot
|