SQRT(3P)

πŸ¦‹ SQRT(3P) β€” bestie edition πŸ¦‹

okay so first things first: this is SQRT(3P), and she lives in the POSIX Programmer's Manual ✨ (that's the <pink>3P</pink> part, iykyk).


🌸 PROLOG 🌸

so before we even get into it β€” heads up, this page is part of the POSIX Programmer's Manual πŸ’–. The Linux implementation of this interface may differ (like, go check the corresponding Linux manual page for the deets on Linux behavior, fr), or honestly the interface may not even be implemented on Linux at all. Not me warning you in advance πŸ™ˆ.


πŸ‘‘ NAME πŸ‘‘

sqrt, sqrtf, sqrtl β€” square root function. that's it, that's the girl. πŸ’…


✨ SYNOPSIS ✨

#include <math.h>

double sqrt(double x);
float sqrtf(float x);
long double sqrtl(long double x);

three icons, one job. πŸŽ€


πŸ’– DESCRIPTION πŸ’–

The functionality described on this reference page is aligned with the ISO C standard. And like, if there's any conflict between what's written here and the ISO C standard? That's unintentional bestie, no cap. This volume of POSIX.1‐2017 defers to the ISO C standard. periodt. πŸ‘‘

These functions shall compute the square root of their argument x, √x. literally the whole vibe. ✨

An application wishing to check for error situations should set errno to zero and call feclearexcept(FE_ALL_EXCEPT) before calling these functions. On return, if errno is non-zero or fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) is non-zero, an error has occurred. so like, set up your little vibe check before you call, okay? πŸ’…


🫢 RETURN VALUE 🫢

Upon successful completion, these functions shall return the square root of x. slay. 🌈

For finite values of x < -0, a domain error shall occur, and either a NaN (if supported), or an implementation-defined value shall be returned. so yeah, negative inputs are giving chaos energy 😭.

If x is NaN, a NaN shall be returned.

If x is Β±0 or +Inf, x shall be returned. (yes bestie, she hands you back exactly what you gave her πŸ’–)

If x is -Inf, a domain error shall occur, and a NaN shall be returned.


πŸ’€ ERRORS πŸ’€

These functions shall fail if:

Domain Error 🚨

The finite value of x is < -0, or x is -Inf. like, don't do that. πŸ™ˆ

If the integer expression (math_errhandling & MATH_ERRNO) is non-zero, then errno shall be set to [EDOM]. If the integer expression (math_errhandling & MATH_ERREXCEPT) is non-zero, then the invalid floating-point exception shall be raised.

The following sections are informative.


🌸 EXAMPLES 🌸

Taking the Square Root of 9.0 ⭐

#include <math.h>
...
double x = 9.0;
double result;
...
result = sqrt(x);

look at her go 🧚 (spoiler: result is 3.0, but that's just math being math, the page didn't say it, so don't @ me πŸ’…)


πŸŽ€ APPLICATION USAGE πŸŽ€

On error, the expressions (math_errhandling & MATH_ERRNO) and (math_errhandling & MATH_ERREXCEPT) are independent of each other β€” like two besties living their own lives πŸ’œ β€” but at least one of them must be non-zero. one of them has to show up to the party, fr fr.


🌈 RATIONALE 🌈

None.

(genuinely nothing. she said no thoughts, head empty πŸ¦‹)


⭐ FUTURE DIRECTIONS ⭐

None.

(no plans, no drama, just vibes ✨)


πŸ‘‘ SEE ALSO πŸ‘‘

  • feclearexcept()
  • fetestexcept()
  • isnan()

The Base Definitions volume of POSIX.1‐2017, Section 4.20, Treatment of Error Conditions for Mathematical Functions, <math.h>, <stdio.h>


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 🫢 this one's brought to you by IEEE/The Open Group, 2017 β€” mwah, take care of her, she's iconic πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from