Rounding modes and standard library functions

On AIX®, C and C++ input/output and conversion functions apply the rounding mode in effect to the values that are input or output by the function. These functions include printf, scanf, atof, and ftoa, as well as the C++ input and output operators (>> and <<) on objects like cin and cout.

For example, if the current rounding mode is round-to-infinity, the printf function will apply that rounding mode to the floating-point digit string value it prints, in addition to the rounding that was already performed on a calculation. The following example illustrates this:
#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;    
          
	fesetround (FE_UPWARD);   
	one_third = one / three;  /* not folded */   
	printf ("1/3 with execution-time rounding to +infinity = %.17f\n", one_third); 

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

	return 0;   
}
When compiled with the default options, this code produces the following results:
1/3 with execution-time rounding to +infinity = 0.33333333333333338
1/3 with execution-time rounding to -infinity = 0.33333333333333337 
In the first calculation, the value returned is rounded upward to 0.33333333333333337, but the printf function rounds this value upward again, to print out 0.33333333333333338. The solution to this problem, which is used in the second calculation, is to reset the rounding mode to round-to-nearest just before the call to the library function is made.