Integer Conversion with Selected Floating-Point Functions

This table shows the effect of various floating point functions related to integer rounding on a few selected values.

The rint() function uses the “current rounding direction”, which can be set by calling fesetround(). The second row denotes (only for rint) which rounding direction was selected before the call to rint(). default means that fesetround() was not called.

A few functions are missing:

  • nearbyint() does the same as rint(), but rint() raises an inexact exception while nearbyint() doesn't (see the man page).
  • nearbyint(), round() and trunc() are not available on Solaris 9.
value floor ceil rint rint rint rint rint
default FE_DOWNWARD FE_TONEAREST FE_TOWARDZERO FE_UPWARD
-NaN -NaN -NaN -NaN -NaN -NaN -NaN -NaN
-Inf -Inf -Inf -Inf -Inf -Inf -Inf -Inf
-19.6999 -20.0 -19.0 -19.0 -20.0 -20.0 -19.0 -19.0
-19.3000 -20.0 -19.0 -19.0 -20.0 -19.0 -19.0 -19.0
-1.6999 -2.0 -1.0 -1.0 -2.0 -2.0 -1.0 -1.0
-1.5000 -2.0 -1.0 -1.0 -2.0 -2.0 -1.0 -1.0
-0.0000 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0 -0.0
0.0000 0.0 0.0 0.0 0.0 0.0 0.0 0.0
0.3000 0.0 1.0 1.0 0.0 0.0 0.0 1.0
0.5000 0.0 1.0 1.0 0.0 0.0 0.0 1.0
0.7000 0.0 1.0 1.0 0.0 1.0 0.0 1.0
1.0000 1.0 1.0 1.0 1.0 1.0 1.0 1.0
1.9000 1.0 2.0 2.0 1.0 2.0 1.0 2.0
Inf Inf Inf Inf Inf Inf Inf Inf
NaN NaN NaN NaN NaN NaN NaN NaN

Creating NaN and Inf in C

There may be constants for it, but this works, too:

const double infinity = 1.0 / 0.0;

const double nan = 1.0 / 0.0 - 1.0 / 0.0;

Negative infinity is just -infinity and negative NaN is -nan.