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
88
|
function abs(x) {
return (x+0 >= 0) ? x : -x
}
function check(x,what, f,res) {
for (f in formats) {
res = sprintf(f,x)
if (formats[f] == "non-decimal") {
if ((x >= 0) && (res !~ /e+/)) {
if (abs(strtonum(res)-x) > 1e-5*abs(x))
printf "(sprintf(%s,%s) = %s)-%g = %g\n",
f,what,res,x,strtonum(res)-x
}
}
else if (abs(res-x) > 1e-5*abs(x))
printf "(sprintf(%s,%s) = %s)-%g = %g\n",
f,what,res,x,res-x
}
}
function check_cons(fmt,base,rot,mexp, i,j,dig,res,s) {
# first off, check that zero formats properly
if ((s = sprintf(fmt,0)) != "0")
printf "(sprintf(%s,0) = %s) != 0\n",fmt,s
res = "1"
dig = 1
j = 0
for (i = 0; i <= mexp; i++) {
s = sprintf(fmt,base^i)
if (s ~ /e+/)
return
if (s != res)
printf "(sprintf(%s,%d^%d) = %s) != %s\n",
fmt,base,i,s,res
if (++j == rot) {
dig = 1
res = ("10"substr(res,2))
j = 0
}
else {
dig *= 2
res = (dig substr(res,2))
}
}
}
BEGIN {
# this doesn't necessarily have to be exact since we're checking
# magnitude rather than precision
if (!HUGEVAL) HUGEVAL = 1.7976931348623157e+308
formats["%s"] = ""
formats["%d"] = ""
formats["%.0f"] = ""
formats["0%o"] = "non-decimal"
formats["0x%x"] = "non-decimal"
check(0,"0")
limit = HUGEVAL / 10
value = 1
for (i = 0; ; i++) {
check(10^i,"10^"i)
check(-10^i,"-10^"i)
#[probably should test value against 10^i here]
if (value >= limit) break
value *= 10
}
limit = HUGEVAL / 2
value = 1
for (i = 0; ; i++) {
check(2^i,"2^"i)
check(-2^i,"-2^"i)
#[probably should test value against 2^i here]
if (value >= limit) break
value *= 2
}
check_cons("%d",10,1,9)
check_cons("%x",2,4,31)
check_cons("%o",2,3,31)
# make sure basic %d and %x are working properly
printf "%d %d %x\n",3.7,-3.7,23.7
# check another problem in gawk 3.1.5: precision over 30 crashes
printf "%.55d\n",1
}
|