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
xis NaN, a domain error shall occur and an unspecified value is returned. - If
xis +Inf, a domain error shall occur and an unspecified value is returned. - If
xis -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
xargument 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, thenerrnoshall 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, thenerrnoshall 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 π¦
π COPYRIGHT π
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
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms