-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_symbol2float.c
87 lines (83 loc) · 2.43 KB
/
test_symbol2float.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(void)
{
char buf[256];
float f;
int i, len, isfloat;
char *strs[] = {
"-345",
"0.00002",
"0.000000000003",
"5e12",
"10.000000000003",
"0",
"0.0",
"0.00000000000",
"000000000000",
".0",
".000000000000000",
"",
"0.0.0",
".0000000000a00000",
"0x00000",
"0x01010",
"+123",
"fourty4",
};
for (i=0; i< 18; i++) {
isfloat = 1;
f = atof(strs[i]);
printf("original %s float: %g\n", strs[i], f);
/*
sprintf(buf, "%g", f);
printf("char: %s\n", buf);
f = atof(buf);
printf("float: %g\n", f);
*/
len = strlen(strs[i]);
if (len == 0) {
isfloat = 0;
printf("zero length\n");
} else if(strs[i][0] == '+') {
isfloat = 0;
printf("start with +\n");
} else if(strs[i][0] == '0' && strs[i][1] == 'x') {
/* pd doesn't support hex 0x format numbers */
isfloat = 0;
printf("hex not supported\n");
} else if (f == 0) {
if(len == 1 && strs[i][0] != '0') {
isfloat = 0;
printf("one length != 0\n");
} else if (len > 1) {
if((strs[i][0] != '0' && strs[i][0] != '.' && strs[i][0] != '-') &&
strs[i][1] != '0') {
isfloat = 0;
printf("> 1 but not .0 or -0\n");
} else {
/* we already checked positions 0 and 1 so skip */
//printf("teststr: '%s' len: %d\n", strs[i], len);
char* str = &strs[i][2];
int founddot = 0;
while (*str != '\0') {
//printf("test:%c ", *str);
if(*str == '.')
founddot++;
if((*str != '0' && *str != '.') || founddot) {
isfloat = 0;
printf("found non-0 or non-.\n");
break;
}
str++;
}
//printf("\n");
}
}
}
// if(!isfloat)
// printf("THIS IS NOT\n");
printf("------------\n");
}
}