Project

General

Profile

Bug #5224 » cflt4.c

Richard PALO, 2014-10-22 06:33 AM

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

    
6
int main(void)
7
{
8
	const int size=100;
9
	int status, i, j, precision;
10
	char buffer[size], check[size];
11
	double val;
12

    
13
	fesetround(FE_TONEAREST);
14

    
15
	for (j=1; j<100; j++) 
16
	{
17
	for (i=2; i<100; i++)
18
	{
19
#ifndef __DBL_DECIMAL_DIG__
20
		for (precision = DBL_DIG-1; precision <= DECIMAL_DIG; precision++)
21
#else
22
		for (precision = DBL_DIG-1; precision <= __DBL_DECIMAL_DIG__; precision++)
23
#endif
24
		{
25
			val = (double)(0.0 + j) / i;
26
			// first get max precision for control check
27
			status = snprintf(check, size, "%+-.*f", DECIMAL_DIG, val);
28
   			if (status <0)
29
				return status;
30
			// then get specific precision
31
			status = snprintf(buffer,size,"%+-#.*f",precision,val);
32
   			if (status <0)
33
				return status;
34

    
35
			if (strlen(check)>strlen(buffer) && 
36
					strncmp(buffer, check, strlen(buffer)))
37
			{	//last check if correctly rounded up 
38
				if (check[strlen(buffer)] < '5' && 
39
					buffer[strlen(buffer)-1]> check[strlen(buffer)-1])
40
	   					printf("failure:f precision %d for %02d/%02d => %s (%s)\n",
41
							precision, j, i, buffer, check);
42
			}
43
		}
44
	}
45
	}
46

    
47
	return 0;
48
}
(5-5/5)