nearbyint
From cppreference.com
Defined in header
<math.h>
|
||
float nearbyintf( float arg );
|
(since C99) | |
double nearbyint( double arg );
|
(since C99) | |
long double nearbyintl( long double arg );
|
(since C99) | |
Rounds the floating-point argument arg
to an integer value in floating-point format, using the current rounding mode.
Contents |
[edit] Parameters
arg | - | floating point value |
[edit] Return value
The integer result of rounding arg
[edit] Notes
The only difference between nearbyint and rint is that rint may raise the FE_INEXACT floating-point exception, while nearbyint never raises it.
[edit] Example
Run this code
#include <stdio.h> #include <math.h> #include <errno.h> #include <fenv.h> #include <string.h> #pragma STDC FENV_ACCESS ON void show_fe_exceptions(void) { printf("current exceptions raised: "); if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO"); if(fetestexcept(FE_INEXACT)) printf(" FE_INEXACT"); if(fetestexcept(FE_INVALID)) printf(" FE_INVALID"); if(fetestexcept(FE_OVERFLOW)) printf(" FE_OVERFLOW"); if(fetestexcept(FE_UNDERFLOW)) printf(" FE_UNDERFLOW"); if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none"); printf("\n"); } void show_fe_rounding_method(void) { printf("current rounding method: "); switch (fegetround()) { case FE_TONEAREST: printf ("FE_TONEAREST"); break; case FE_DOWNWARD: printf ("FE_DOWNWARD"); break; case FE_UPWARD: printf ("FE_UPWARD"); break; case FE_TOWARDZERO: printf ("FE_TOWARDZERO"); break; default: printf ("unknown"); }; printf("\n"); } int main(void) { /* current rounding method: to nearest */ /* If the result is midway between two representable values, the even */ /* representable is chosen. */ show_fe_rounding_method(); printf("\n"); printf("nearbyint(+2.5) = %+.1f\n", nearbyint(+2.5)); /* even representable */ printf("nearbyint(-2.5) = %+.1f\n", nearbyint(-2.5)); printf("nearbyint(+1.5) = %+.1f\n", nearbyint(+1.5)); /* even representable */ printf("nearbyint(-1.5) = %+.1f\n", nearbyint(-1.5)); printf("nearbyint(+0.0) = %+.1f\n", nearbyint(+0.0)); /* all rounding modes */ printf("nearbyint(-0.0) = %+.1f\n", nearbyint(-0.0)); /* " */ printf("nearbyint(+INFINITY) = %+f\n", nearbyint(+INFINITY)); /* " */ printf("nearbyint(-INFINITY) = %+f\n", nearbyint(-INFINITY)); /* " */ printf("nearbyint(NAN) = %f\n", nearbyint(NAN)); /* " */ printf("\n"); /* no "inexact" floating-point exception for finite non-integer arguments */ errno = 0; feclearexcept(FE_ALL_EXCEPT); printf("nearbyint(2.5) = %.1f\n", nearbyint(2.5)); printf("%s\n",strerror(errno)); show_fe_exceptions(); return 0; }
Possible output:
current rounding method: FE_TONEAREST nearbyint(+2.5) = +2.0 nearbyint(-2.5) = -2.0 nearbyint(+1.5) = +2.0 nearbyint(-1.5) = -2.0 nearbyint(+0.0) = +0.0 nearbyint(-0.0) = -0.0 nearbyint(+INFINITY) = +inf nearbyint(-INFINITY) = -inf nearbyint(NAN) = nan nearbyint(2.5) = 2.0 Success current exceptions raised: none
[edit] See also
(C99)(C99)(C99)(C99)(C99)(C99)(C99)(C99)(C99)
|
rounds to an integer using current rounding mode with exception if the result differs (function) |
(C99)(C99)(C99)
|
rounds to nearest integer, rounding away from zero in halfway cases (function) |
(C99)(C99)
|
gets or sets rounding direction (function) |
C++ documentation for nearbyint
|