Namespaces
Variants
Views
Actions

nearbyint

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
nearbyint
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
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

#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)
nearest integer using current rounding mode with
exception if the result differs
(function) [edit]
(C99)(C99)(C99)
nearest integer, rounding away from zero in halfway cases
(function) [edit]
gets or sets rounding direction
(function) [edit]
C++ documentation for nearbyint