Check out the standard header <fenv.c>
, specifically the fesetround()
function and the four macros FE_DOWNWARD
, FE_TOWARDZERO
, FE_TONEAREST
and FE_UPWARD
. This controls how floating point values are rounded to integers. Make sure your implementation (i.e., C compiler / C library) actually support this (by checking the return value of fesetround()
and the documentation of your implementation).
Functions honoring these settings include (from <math.h>
):
llrint()
llrintf()
llrintl()
lrint()
lrintf()
lrintl()
rint()
rintf()
rintl()
llround()
llroundf()
llroundl()
lround()
lroundf()
lroundl()
nearbyint()
nearbyintf()
nearbyintl()
depending on your needs (parameter type and return type, with or without inexact floating point exception).
NOTE: round()
, roundf()
and roundl()
do look like they belong in the list above, but these three do not honor the rounding mode set by fesetround()
!!
Refer to your most favourite standard library documentation for the exact details.