From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/numeric%2Fmath%2Frint.html | 134 +++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 devdocs/c/numeric%2Fmath%2Frint.html (limited to 'devdocs/c/numeric%2Fmath%2Frint.html') diff --git a/devdocs/c/numeric%2Fmath%2Frint.html b/devdocs/c/numeric%2Fmath%2Frint.html new file mode 100644 index 00000000..cd195c3e --- /dev/null +++ b/devdocs/c/numeric%2Fmath%2Frint.html @@ -0,0 +1,134 @@ +

rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl

Defined in header <math.h>
float rintf( float arg );
+
(1) (since C99)
double rint( double arg );
+
(2) (since C99)
long double rintl( long double arg );
+
(3) (since C99)
Defined in header <tgmath.h>
#define rint( arg )
+
(4) (since C99)
Defined in header <math.h>
long lrintf( float arg );
+
(5) (since C99)
long lrint( double arg );
+
(6) (since C99)
long lrintl( long double arg );
+
(7) (since C99)
Defined in header <tgmath.h>
#define lrint( arg )
+
(8) (since C99)
Defined in header <math.h>
long long llrintf( float arg );
+
(9) (since C99)
long long llrint( double arg );
+
(10) (since C99)
long long llrintl( long double arg );
+
(11) (since C99)
Defined in header <tgmath.h>
#define llrint( arg )
+
(12) (since C99)
+1-3) Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode.
+5-7, 9-11) Rounds the floating-point argument arg to an integer value in integer format, using the current rounding mode.
+4,8,12) Type-generic macros: If arg has type long double, rintl, lrintl, llrintl is called. Otherwise, if arg has integer type or the type double, rint, lrint, llrint is called. Otherwise, rintf, lrintf, llrintf is called, respectively.

Parameters

+ +
arg - floating point value

Return value

If no errors occur, the nearest integer value to arg, according to the current rounding mode, is returned.

+

Error handling

Errors are reported as specified in math_errhandling.

+

If the result of lrint or llrint is outside the range representable by the return type, a domain error or a range error may occur.

+

If the implementation supports IEEE floating-point arithmetic (IEC 60559), For the rint function:

+ For lrint and llrint functions:

Notes

POSIX specifies that all cases where lrint or llrint raise FE_INEXACT are domain errors.

+

As specified in math_errhandling, FE_INEXACT may be (but isn't required to be on non-IEEE floating-point platforms) raised by rint when rounding a non-integer finite value.

+

The only difference between rint and nearbyint is that nearbyint never raises FE_INEXACT.

+

The largest representable floating-point values are exact integers in all standard floating-point formats, so rint never overflows on its own; however the result may overflow any integer type (including intmax_t), when stored in an integer variable.

+

If the current rounding mode is...

+

Example

#include <stdio.h>
+#include <math.h>
+#include <fenv.h>
+#include <limits.h>
+ 
+int main(void)
+{
+#pragma STDC FENV_ACCESS ON
+    fesetround(FE_TONEAREST);
+    printf("rounding to nearest (halfway cases to even):\n"
+           "rint(+2.3) = %+.1f  ", rint(2.3));
+    printf("rint(+2.5) = %+.1f  ", rint(2.5));
+    printf("rint(+3.5) = %+.1f\n", rint(3.5));
+    printf("rint(-2.3) = %+.1f  ", rint(-2.3));
+    printf("rint(-2.5) = %+.1f  ", rint(-2.5));
+    printf("rint(-3.5) = %+.1f\n", rint(-3.5));
+ 
+    fesetround(FE_DOWNWARD);
+    printf("rounding down: \nrint(+2.3) = %+.1f  ", rint(2.3));
+    printf("rint(+2.5) = %+.1f  ", rint(2.5));
+    printf("rint(+3.5) = %+.1f\n", rint(3.5));
+    printf("rint(-2.3) = %+.1f  ", rint(-2.3));
+    printf("rint(-2.5) = %+.1f  ", rint(-2.5));
+    printf("rint(-3.5) = %+.1f\n", rint(-3.5));
+    printf("rounding down with lrint: \nlrint(+2.3) = %ld  ", lrint(2.3));
+    printf("lrint(+2.5) = %ld  ", lrint(2.5));
+    printf("lrint(+3.5) = %ld\n", lrint(3.5));
+    printf("lrint(-2.3) = %ld  ", lrint(-2.3));
+    printf("lrint(-2.5) = %ld  ", lrint(-2.5));
+    printf("lrint(-3.5) = %ld\n", lrint(-3.5));
+ 
+    printf("lrint(-0.0) = %ld\n", lrint(-0.0));
+    printf("lrint(-Inf) = %ld\n", lrint(-INFINITY)); // FE_INVALID raised
+ 
+    // error handling
+    feclearexcept(FE_ALL_EXCEPT);
+    printf("rint(1.1) = %.1f\n", rint(1.1));
+    if(fetestexcept(FE_INEXACT)) puts("    FE_INEXACT was raised");
+ 
+    feclearexcept(FE_ALL_EXCEPT);
+    printf("lrint(LONG_MIN-2048.0) = %ld\n", lrint(LONG_MIN-2048.0));
+    if(fetestexcept(FE_INVALID)) puts("    FE_INVALID was raised");
+}

Possible output:

+
rounding to nearest (halfway cases to even):
+rint(+2.3) = +2.0  rint(+2.5) = +2.0  rint(+3.5) = +4.0
+rint(-2.3) = -2.0  rint(-2.5) = -2.0  rint(-3.5) = -4.0
+rounding down: 
+rint(+2.3) = +2.0  rint(+2.5) = +2.0  rint(+3.5) = +3.0
+rint(-2.3) = -3.0  rint(-2.5) = -3.0  rint(-3.5) = -4.0
+rounding down with lrint: 
+lrint(+2.3) = 2  lrint(+2.5) = 2  lrint(+3.5) = 3
+lrint(-2.3) = -3  lrint(-2.5) = -3  lrint(-3.5) = -4
+lrint(-0.0) = 0
+lrint(-Inf) = -9223372036854775808
+rint(1.1) = 1.0
+    FE_INEXACT was raised
+lrint(LONG_MIN-2048.0) = -9223372036854775808
+    FE_INVALID was raised

References

See also

+ + + +
+
(C99)(C99)(C99)
rounds to nearest integer not greater in magnitude than the given value
(function)
+
(C99)(C99)(C99)
rounds to an integer using current rounding mode
(function)
+
(C99)(C99)
gets or sets rounding direction
(function)
C++ documentation for rint
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/numeric/math/rint +

+
-- cgit v1.2.3