LROUND(3P)

✨ LROUND(3P) ✨

okay bestie, welcome to the POSIX Programmer's Manual page for lround, lroundf, and lroundl πŸ’– this is the tea on rounding numbers to the nearest integer, and honestly? it's giving "surprisingly dramatic for a math function." let's get into it 🫢


🌸 PROLOG 🌸

This manual page is part of the POSIX Programmer's Manual. Heads up, bestie: the Linux implementation of this interface may differ (so go check the corresponding Linux manual page for the deets on Linux behavior), or the interface might not even be implemented on Linux at all. 😭 no cap.


πŸ’… NAME πŸ’…

lround, lroundf, lroundl β€” round to nearest integer value. that's it. that's the whole job. iconic behavior honestly πŸ‘‘


✨ SYNOPSIS ✨

#include <math.h>

long lround(double x);
long lroundf(float x);
long lroundl(long double x);

three functions, one mission, three different float flavors πŸ“


πŸ¦‹ DESCRIPTION πŸ¦‹

So the functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional, okay?? nobody's fighting. This volume of POSIX.1‐2017 defers to the ISO C standard. periodt.

These functions shall round their argument to the nearest integer value, rounding halfway cases away from zero, regardless of the current rounding direction. literally it does not care what the rounding direction is set to, it has its own opinions πŸ’…

Now, if your application wants to check for error situations, here's the little ritual: set errno to zero and call feclearexcept(FE_ALL_EXCEPT) before calling these functions. 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. manifesting an error check ✨


πŸŽ€ RETURN VALUE πŸŽ€

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

But the drama? oh the drama 😭:

  • 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, an unspecified value shall be returned. On systems that support the IEC 60559 Floating-Point option, a domain shall occur; otherwise, a domain error may occur.
  • If the correct value is negative and too large to represent as a long, an unspecified value shall be returned. On systems that support the IEC 60559 Floating-Point option, a domain shall occur; otherwise, a domain error may occur.

like, the answer is just "unspecified" bestie, don't look at it too hard πŸ™ˆ


πŸ’€ 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. 🫢 (so like, cute extra reading, not the binding rules)

🌈 EXAMPLES 🌈

None. 😭 not a single one. we're on our own out here bestie.


πŸ’œ APPLICATION USAGE πŸ’œ

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 independent but they're also not allowed to both be zero. it's giving "we're not codependent but you literally cannot leave me" πŸ’…


πŸ”₯ RATIONALE πŸ”₯

These functions differ from the lrint() functions in the default rounding direction β€” with the lround() functions rounding halfway cases away from zero and needing not to raise the inexact floating-point exception for non-integer arguments that round to within the range of the return type. so if you were wondering why both exist, this is why. the more you know ⭐


πŸ‘‘ FUTURE DIRECTIONS πŸ‘‘

None. nothing coming. the future is quiet. we love a stable API πŸŽ€


🌸 SEE ALSO 🌸

  • feclearexcept()
  • fetestexcept()
  • llround()

And from the Base Definitions volume of POSIX.1‐2017:

  • Section 4.20, Treatment of Error Conditions for Mathematical Functions
  • <math.h>

iykyk, go read those if you want the full lore πŸ¦‹


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. (not me being the messenger for the typo hotline 😭)


okay bestie that's the whole page, every last flag and caveat accounted for, no cap πŸ’…

IEEE/The Open Group β€” 2017 β€” LROUND(3P) 🌈✨ mwah, go round some numbers responsibly 🫢

πŸ“¦ where this page comes from