LLRINT(3POSIX)

🌸 LLRINT β€” POSIX Programmer's Manual, bestie edition 🌸

okay so real quick, this is llrint(3POSIX) from the POSIX Programmer's Manual. Buckle up, we're rounding floats and we're doing it with taste. πŸ’…


✨ PROLOG ✨

So this page? It's part of the POSIX Programmer's Manual, periodt.

Heads up bestie: the Linux implementation of this interface may differ (so like, go consult the corresponding Linux manual page if you wanna know how Linux actually behaves), or honestly the interface may not be implemented on Linux at all. Vibe check accordingly. πŸ’€

πŸŽ€ NAME πŸŽ€

llrint, llrintf, llrintl β€” round to the nearest integer value using the current rounding direction. That's the whole job. Iconic, honestly. πŸ‘‘


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <math.h>

long long llrint(double x);
long long llrintf(float x);
long long llrintl(long double x);

Three flavors, one destiny. πŸ“


πŸ’– DESCRIPTION πŸ’–

The functionality on this reference page is aligned with the ISO C standard. If anything here conflicts with the ISO C standard, that was not the intention β€” not even a little. This volume of POSIX.1‐2017 defers to the ISO C standard. It's giving "the ISO C standard is the main character here." ✨

These functions shall round their argument to the nearest integer value, rounding according to the current rounding direction. (Yes, the rounding direction. It's a whole thing. It matters.)

Now, if you're an application that wants to actually check for error situations, here's the little ritual you do:

  • set errno to zero
  • call feclearexcept(FE_ALL_EXCEPT)

...and you do all that before calling these functions. Then on return, you look at it like this: if errno is non-zero, or if fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) is non-zero β€” an error has occurred. Boom. Detected. πŸ”


🌈 RETURN VALUE 🌈

Upon successful completion, these functions shall return the rounded integer value. slay.

Now the messier scenarios, because math is dramatic:

  • If x is NaN, a domain error shall occur, and an unspecified value is returned. πŸ™ˆ
  • If x is +Inf, a domain error shall occur and an unspecified value is returned.
  • If x is -Inf, a domain error shall occur and an unspecified value is returned.
  • If the correct value is positive and too large to represent as a long long, an unspecified value shall be returned. On systems that support the IEC 60559 Floating-Point option, a domain error shall occur; otherwise, a domain error may occur.
  • If the correct value is negative and too large to represent as a long long, an unspecified value shall be returned. On systems that support the IEC 60559 Floating-Point option, a domain error shall occur; otherwise, a domain error may occur.
"unspecified value" is doing so much heavy lifting here and honestly it's the most Gen Z thing in this whole page. it's like, we're not gonna say. πŸ’€

πŸ”₯ ERRORS πŸ”₯

These functions shall fail if:

  • Domain Error β€” The x argument is NaN or Β±Inf, or the correct value is not representable as an integer.
    • 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.

These functions may fail if:

  • Domain Error β€” The correct value is not representable as an integer.
    • 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. (i.e. they're the vibes section, not the rules section. still useful tho. 🫢)

🧚 EXAMPLES 🧚

None.

Yeah. Nothing. Not a single one. The audacity. πŸ’…


⭐ APPLICATION USAGE ⭐

On error, the expressions (math_errhandling & MATH_ERRNO) and (math_errhandling & MATH_ERREXCEPT) are independent of each other β€” like two besties who don't share a calendar β€” but at least one of them must be non-zero. That's the rule. ✨


πŸ’œ RATIONALE πŸ’œ

These functions provide floating-to-integer conversions. They round according to the current rounding direction. If the rounded value is outside the range of the return type, the numeric result is unspecified and the invalid floating-point exception is raised. And when they raise no other floating-point exception and the result differs from the argument, they raise the inexact floating-point exception. (Inexact! So real of them. 🎯)


🌸 FUTURE DIRECTIONS 🌸

None.

The future? Empty. Peaceful. Unbothered. 😌


πŸ‘‘ SEE ALSO πŸ‘‘

  • feclearexcept()
  • fetestexcept()
  • lrint()

And from the Base Definitions volume of POSIX.1‐2017: Section 4.20, Treatment of Error Conditions for Mathematical Functions, and <math.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. (So like, if they disagree, the original wins. no arguments. periodt.)

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 β€” brought to you by IEEE/The Open Group, dated 2017, under the manual LLRINT(3POSIX). go round some integers, stay in your current rounding direction, and drink water. mwah. πŸ’…βœ¨πŸŽ€