HYPOT(3P)

πŸ’– HYPOT(3P) πŸ’– β€” it's giving POSIX Programmer's Manual, and it's giving math

okay bestie, sit down, hydrate, because we're about to talk about the hypotenuse. Yes, THE hypotenuse. The main character of every right triangle. This page is from the POSIX Programmer's Manual, so let's get into it. 🫢


🌸 PROLOG 🌸

so like, context first:

  • This manual page is part of the POSIX Programmer's Manual.
  • The Linux implementation of this interface may differ β€” so go consult the corresponding Linux manual page if you need the Linux deets.
  • Or, plot twist, the interface may not be implemented on Linux at all. πŸ™ˆ

✨ NAME ✨

hypot, hypotf, hypotl β€” the Euclidean distance function. That's it, that's the name section, slay. πŸ’…


πŸŽ€ 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);

three flavors, one vibe. ✨


πŸ’… DESCRIPTION πŸ’…

okay so the functionality on this reference page is aligned with the ISO C standard. If there's any conflict between what's written here and the ISO C standard, that conflict is unintentional β€” no drama intended. And this volume of POSIX.1‐2017 literally defers to the ISO C standard. periodt.

These functions shall compute the value of the square root of x2+y2 β€” without undue overflow or underflow. She's careful, she's precise, she does not spill.

Now, if you're an application that wants to check for error situations, here's the ritual: 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 β€” boom, an error has occurred. πŸ’€


πŸ¦‹ 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.
  • If x or y is Β±Inf, +Inf shall be returned β€” even if one of x or y is NaN. Infinity wins, no cap.
  • If x or y is NaN, and the other is not Β±Inf, a NaN shall be returned.
  • If both arguments are subnormal and the correct result is subnormal, a range error may occur and the correct result shall be returned.

πŸ”₯ 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.
heads up bestie: the following sections are informative. not requirements, just lore.

🌈 EXAMPLES 🌈

See the EXAMPLES section in atan2(). that's the whole section. she's brief, she's efficient, she ate. πŸ’…


πŸ’œ APPLICATION USAGE πŸ’œ

the little equivalences that live in my head rent free:

  • hypot(x,y), hypot(y,x), and hypot(x, -y) are equivalent. order? doesn't matter. sign? doesn't matter. iconic behavior.
  • hypot(x, Β±0) is equivalent to fabs(x).
  • Underflow only happens when both x and y are subnormal and the (inexact) result is also subnormal.
  • These functions take precautions against overflow during intermediate steps of the computation. she's thinking 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.

⭐ RATIONALE ⭐

None. πŸ’€ literally nothing. moving on.


🧚 FUTURE DIRECTIONS 🧚

None. also nothing. we love a chill future. πŸ¦‹


πŸ“ SEE ALSO πŸ“

  • atan2()
  • feclearexcept()
  • fetestexcept()
  • isnan()
  • sqrt()
  • The Base Definitions volume of POSIX.1‐2017, Section 4.20, Treatment of Error Conditions for Mathematical Functions, <math.h>

okay legal bestie moment, staying accurate because facts don't change even when the vibe does:

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 .


and that's the whole page, bestie 🫢 courtesy of IEEE/The Open Group, dated 2017, filed under HYPOT(3P). go forth and compute your hypotenuses safely, no overflow, no underflow, only slay. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from