Project

General

Profile

Bug #5224 » cflt2.c

Richard PALO, 2014-10-10 04:13 PM

 
1
#include <float.h>
2
#include <fenv.h>
3
#include <stdio.h>
4

    
5
int main(void)
6
{
7

    
8
printf("FLT_ROUNDS = %d, fegetround()=%d\n", FLT_ROUNDS, fegetround());
9
#ifdef	FLT_EVAL_METHOD		
10
		printf("FLT_EVAL_METHOD = %d\n", FLT_EVAL_METHOD);
11
#else
12
		printf("FLT_EVAL_METHOD not defined\n");
13
#endif
14
		printf("DBL_MANT_DIG = %d\n", DBL_MANT_DIG);
15
#ifdef	DECIMAL_DIG
16
		printf("DECIMAL_DIG = %d\n", DECIMAL_DIG);
17
#else
18
		printf("DECIMAL_DIG not defined\n");
19
#endif
20
		printf("DBL_DIG = %d\n", DBL_DIG);
21

    
22
		const int size=100;
23
		int status, i, precision;
24
		char buffer[size];
25
		double val=1.0/3.0;
26
		
27
		for ( i=0; i<4;i++) 
28
		{
29
			if (i==0)
30
				fesetround(FE_TONEAREST), printf("fesetround(FE_TONEAREST)\n");
31
			else if (i==1)
32
				fesetround(FE_DOWNWARD), printf("fesetround(FE_DOWNWARD)\n");
33
			else if (i==2)
34
				fesetround(FE_UPWARD), printf("fesetround(FE_UPWARD)\n");
35
			else if (i==3)
36
				fesetround(FE_TOWARDZERO), printf("fesetround(FE_TOWARDZERO)\n");
37

    
38
			for (precision= DBL_DIG ; precision<=22; precision++)
39
			{
40
				status = snprintf(buffer,size,"%+-#.*e",precision,val);
41
				if (status <0)
42
					return status;
43
				printf("e precision %d => %s\n",precision,buffer);
44

    
45
				status = snprintf(buffer,size,"%+-#.*f",precision,val);
46
        		if (status <0)
47
       				return status;
48
        		printf("f precision %d => %s\n",precision,buffer);
49
			}
50
		}
51
		return 0;
52
}
(1-1/5)