Matching compile-time and runtime rounding modes

The default rounding mode used at compile time and run time is round-to-nearest, ties even. If your program changes the rounding mode at run time, the results of a floating-point calculation might be slightly different from those that are obtained at compile time. The following example illustrates this:1
#include <float.h>
#include <fenv.h>
#include <stdio.h>

int main ( )
{
	volatile double one = 1.f, three = 3.f;  /* volatiles are not folded */
	double one_third;

	one_third = 1. / 3.;  /* folded */
	printf ("1/3 with compile-time rounding = %.17f\n", one_third);

	fesetround (FE_TOWARDZERO);
	one_third = one / three;  /* not folded */
	fesetround (FE_TONEAREST);2
	printf ("1/3 with execution-time rounding to zero = %.17f\n", one_third);

	fesetround (FE_TONEAREST);
	one_third = one / three;  /* not folded */
	fesetround (FE_TONEAREST);2
	printf ("1/3 with execution-time rounding to nearest = %.17f\n", one_third);

	fesetround (FE_UPWARD);
	one_third = one / three;  /* not folded */
	fesetround (FE_TONEAREST);2
	printf ("1/3 with execution-time rounding to +infinity = %.17f\n", one_third);

	fesetround (FE_DOWNWARD);
	one_third = one / three;  /* not folded */
	fesetround (FE_TONEAREST);2
	printf ("1/3 with execution-time rounding to -infinity = %.17f\n", one_third);
	
	return 0;
 }
Notes:
  1. On AIX®, this example must be linked with the system math library, libm, to obtain the functions and macros declared in the fenv.h header file.
  2. See Rounding modes and standard library functions for an explanation of the resetting of the round mode before the call to printf.
When compiled with the default options, this code produces the following results:
1/3 with compile-time rounding = 0.33333333333333331
1/3 with execution-time rounding to zero = 0.33333333333333331
1/3 with execution-time rounding to nearest   = 0.33333333333333331
1/3 with execution-time rounding to +infinity = 0.33333333333333337
1/3 with execution-time rounding to -infinity = 0.33333333333333331 
Because the fourth computation changes the rounding mode to round-to-infinity, the results are slightly different from the first computation, which is performed at compile time, using round-to-nearest. If you do not use the -qfloat=nofold option to suppress all compile-time folding of floating-point computations, it is recommended that you use the -y compiler option with the appropriate suboption to match compile-time and runtime rounding modes. In the previous example, compiling with -yp (round-to-infinity) produces the following result for the first computation:
1/3 with compile-time rounding = 0.33333333333333337

In general, if the rounding mode is changed to +infinity or -infinity, or to any decimal floating-point only rounding mode, it is recommended that you also use the -qfloat=rrm option.