rint


rint

Rounds a floating-point number to an integer value

 #include <math.h> double rint ( double x  ); float rintf ( float x  ); long double rintl ( long double x  ); 

The rint( ) functions round a floating-point number to the next integer value in the current rounding direction. The current rounding direction is an attribute of the floating-point environment that you can read and modify using the fegetround( ) and fesetround( ) functions. The rint( ) functions are similar to the nearbyint( ) functions, except that the rint( ) functions may raise the FE_INEXACT exception (depending on the implementation) when the result of the rounding is different from the argument.

Example

 struct round_modes { int id; char *str; }  arrModes[ ] = {   #ifdef FE_TONEAREST    { FE_TONEAREST, "FE_TONEAREST: round to nearest representable value" },   #endif   #ifdef FE_DOWNWARD    { FE_DOWNWARD, "FE_DOWNWARD: round toward -Inf" },   #endif   #ifdef FE_UPWARD    { FE_UPWARD, "FE_UPWARD: round toward +Inf" },   #endif   #ifdef FE_TOWARDZERO    { FE_TOWARDZERO, "FE_TOWARDZERO: round toward 0" }   #endif }; int nModes = sizeof( arrModes) / sizeof(*arrModes);  #pragma STDC FENV_ACCESS ON for ( int i = 0; i < nModes; ++i) {   if ( fesetround( arrModes[i].id) != 0)     break;   printf( "Current rounding mode: %s\n", arrModes[i].str );   printf( "rint(1.4)  = %4.1f    rint(1.5)  = %4.1f\n",           rint(1.4), rint(1.5) );   printf( "rint(-1.4) = %4.1f    rint(-1.5) = %4.1f\n",           rint(-1.4), rint(-1.5) ); } 

If the implementation supports all four rounding modes, this code produces the following output:

 Current rounding mode: FE_TONEAREST: round to nearest representable value rint(1.4)  =  1.0    rint(1.5)  =  2.0 rint(-1.4) = -1.0    rint(-1.5) = -2.0 Current rounding mode: FE_DOWNWARD: round toward -Inf rint(1.4)  =  1.0    rint(1.5)  =  1.0 rint(-1.4) = -2.0    rint(-1.5) = -2.0 Current rounding mode: FE_UPWARD: round toward +Inf rint(1.4)  =  2.0    rint(1.5)  =  2.0 rint(-1.4) = -1.0    rint(-1.5) = -1.0 Current rounding mode: FE_TOWARDZERO: round toward 0 rint(1.4)  =  1.0    rint(1.5)  =  1.0 rint(-1.4) = -1.0    rint(-1.5) = -1.0 

See Also

lrint( ), llrint( ); nearbyint( ), nexttoward( ), nextafter( ); round( ), lround( ), llround( ), ceil( ), floor( ), fegetround( ), fesetround( )



C(c) In a Nutshell
C in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596006977
EAN: 2147483647
Year: 2006
Pages: 473

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net