COS(3POSIX)

πŸ’– COS(3POSIX) β€” the POSIX Programmer's Manual, bestie edition πŸŽ€

okay so welcome to cos(3POSIX), from the POSIX Programmer's Manual. let's get into it 🌸


✨ PROLOG ✨

okay so first things first, bestie, real talk: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β€” so pls go consult the corresponding Linux manual page for the actual Linux behavior, okay? or, alternatively, the interface might just not be implemented on Linux at all πŸ™ˆ. no cap, that's a real possibility.


🌈 NAME 🌈

cos, cosf, cosl β€” the cosine function. it's giving trigonometry, it's giving math girl summer πŸ‘‘


⭐ SYNOPSIS ⭐

#include <math.h>

double cos(double x);
float cosf(float x);
long double cosl(long double x);

three icons, three float sizes. we love range πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so the functionality described on this reference page is aligned with the ISO C standard. and like, if there's any conflict between the requirements described here and the ISO C standard? that's unintentional, fr fr. this volume of POSIX.1‐2017 defers to the ISO C standard. periodt.

these functions shall compute the cosine of their argument x, measured in radians ✨ (yes, radians, not degrees, i know, i know, i'm sorry, that's just the deal).

and okay, here's the vibe check part for the girlies who want to actually detect errors: an application wishing to check for error situations 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. that's the check. do the check. 🫢


πŸ’œ RETURN VALUE πŸ’œ

upon successful completion, these functions shall return the cosine of x. slay.

but the edge cases are where it gets interesting, so let's go case by case:

  • if x is NaN, a NaN shall be returned. (garbage in, garbage out, but make it standardized πŸ’€)
  • if x is Β±0, the value 1.0 shall be returned.
  • if x is Β±Inf, a domain error shall occur, and a NaN shall be returned.

😭 ERRORS 😭

these functions shall fail if:

  • Domain Error β€” the x argument is Β±Inf.
    • 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.

heads up bestie: the following sections are informative. they're the vibes, not the requirements. πŸ’–

🌸 EXAMPLES 🌸

Taking the Cosine of a 45-Degree Angle

like yes, radians are the law, but if you've got degrees, you convert. iconic behavior:

#include <math.h>
...
double radians = 45 * M_PI / 180;
double result;
...
result = cos(radians);

look at her go. converting units AND computing cosine. main character energy πŸ‘‘


πŸŽ€ APPLICATION USAGE πŸŽ€

real talk, these functions may lose accuracy when their argument is near an odd multiple of Ο€/2, or is far from 0. so like, don't come for her if precision gets a little wobbly way out there, okay? 😭

and also, 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. so they don't talk to each other, they just both have to show up. boundaries, honestly 🫢


πŸ’… RATIONALE πŸ’…

None.

(no notes. she said nothing. and that's fine.)


🌈 FUTURE DIRECTIONS 🌈

None.

(also nothing. the future is, apparently, unbothered.)


πŸ¦‹ SEE ALSO πŸ¦‹

  • acos()
  • feclearexcept()
  • fetestexcept()
  • isnan()
  • sin()
  • tan()

and also: the Base Definitions volume of POSIX.1‐2017, Section 4.20, Treatment of Error Conditions for Mathematical Functions, and <math.h>. 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 .


IEEE/The Open Group β€” 2017 β€” COS(3POSIX)

and that's the whole page bestie, ty for coming to my ted talk, go compute some cosines, stay in radians, and remember: Β±Inf will NOT be tolerated πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά