HYPOT(3POSIX)

πŸ’– HYPOT(3POSIX) β€” ok bestie, let's talk triangles πŸ’–

So this lil cutie lives in the POSIX Programmer's Manual (the 2017 edition, from IEEE / The Open Group, no big deal πŸ’…). Scroll to the very bottom for the whole copyright tea, but first:

🌸 PROLOG 🌸

This manual page is part of the POSIX Programmer's Manual. Heads up though, bestie: the Linux implementation of this interface may differ β€” so like, go consult the corresponding Linux manual page for the deets on Linux behavior. Orrrr the interface might not be implemented on Linux at all. It's giving mystery πŸ™ˆ.


✨ NAME ✨

hypot, hypotf, hypotl β€” the Euclidean distance function, aka the "how far apart are these two things" girlies πŸ¦‹


πŸŽ€ SYNOPSIS πŸŽ€

#include <math.h>

double hypot(double x, double y);
float hypotf(float x, float y);
long double hypotl(long double x, long double y);

Look at them. Three sisters. All iconic. πŸ‘‘


πŸ’œ DESCRIPTION πŸ’œ

Okay so first things first: the functionality described on this reference page is aligned with the ISO C standard. If anything here conflicts with the ISO C standard, that conflict is unintentional β€” this volume of POSIX.1‐2017 defers to the ISO C standard. So like, ISO C is the main character here, we're just the group chat. πŸ’…

These functions shall compute the value of the square root of x2+y2 β€” yes that's x-squared plus y-squared under a root, the whole Pythagorean moment β€” without undue overflow or underflow. She's careful. She's precise. She ate. πŸ“

Now, if you're an application and you wanna check for error situations (very valid of you), you should set errno to zero and call feclearexcept(FE_ALL_EXCEPT) before calling these functions. Then on return: if errno is non-zero, or if fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) is non-zero, then an error has occurred. Vibe check failed, basically. 😭


πŸ”₯ RETURN VALUE πŸ”₯

Upon successful completion, these functions shall return the length of the hypotenuse of a right-angled triangle with sides of length x and y. Geometry class could NEVER. πŸ“

If the correct value would cause overflow, a range error shall occur, and hypot(), hypotf(), and hypotl() shall return the value of the macro HUGE_VAL, HUGE_VALF, and HUGE_VALL, respectively. Big number says "I cannot". πŸ’€

  • If x or y is Β±Inf, +Inf shall be returned β€” even if one of x or y is NaN. Inf is that girl, she wins every argument.
  • If x or y is NaN, and the other is not Β±Inf, a NaN shall be returned. Rude, but fair.
  • If both arguments are subnormal and the correct result is subnormal, a range error may occur, and the correct result shall be returned. She's doing her best okay 🫢

😭 ERRORS 😭

These functions shall fail if:

  • Range Error β€” The result overflows.
    • If the integer expression (math_errhandling & MATH_ERRNO) is non-zero, then errno shall be set to [ERANGE].
    • If the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, then the overflow floating-point exception shall be raised.

These functions may fail if:

  • Range Error β€” The result underflows.
    • If the integer expression (math_errhandling & MATH_ERRNO) is non-zero, then errno shall be set to [ERANGE].
    • If the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, then the underflow floating-point exception shall be raised.

The following sections are informative β€” so like, the gossip portion of the page, not the binding contract πŸ’…


🌈 EXAMPLES 🌈

See the EXAMPLES section in atan2(). That's it. That's the whole section. She's minimal, she's mysterious, go read your other bestie's page. πŸ™ˆ


🧚 APPLICATION USAGE 🧚

  • hypot(x,y), hypot(y,x), and hypot(x, -y) are equivalent. Order doesn't matter, sign doesn't matter, she loves all her inputs equally πŸ’–
  • hypot(x, Β±0) is equivalent to fabs(x). Cute.
  • Underflow only happens when both x and y are subnormal and the (inexact) result is also subnormal. Very specific. Very rare. Very demure.
  • These functions take precautions against overflow during intermediate steps of the computation. She's not gonna blow up mid-math, she plans ahead. ✨
  • On error, the expressions (math_errhandling & MATH_ERRNO) and (math_errhandling & MATH_ERREXCEPT) are independent of each other, but at least one of them must be non-zero. They're not texting each other, but they're both in the group chat. Periodt.

πŸ¦‹ RATIONALE πŸ¦‹

None.

Truly nothing to say. Silent. Stunning. πŸ‘‘


⭐ FUTURE DIRECTIONS ⭐

None.

She's not changing. She's perfect the way she is. πŸ’…


πŸ‘‘ SEE ALSO πŸ‘‘

  • atan2()
  • feclearexcept()
  • fetestexcept()
  • isnan()
  • sqrt()

Also, from the Base Definitions volume of POSIX.1‐2017: Section 4.20, Treatment of Error Conditions for Mathematical Functions, and <math.h>. Go read those too, bestie, iykyk 🫢


Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .

okay that's the whole page bestie, mwah πŸ’–

This one's brought to you by IEEE / The Open Group, dated 2017, and it's the HYPOT(3POSIX) page β€” go forth and compute your hypotenuses safely, no cap. πŸŒΈβœ¨πŸŽ€