Debian Codegraph @

internal/decoder/pow10_test.go

raw
1 package decoder
2
3 import (
4 "math"
5 "math/big"
6 "math/rand"
7 "strconv"
8 "testing"
9 )
10
11 // mulPow10 gives the float64 of strconv.ParseFloat, bit for bit, whenever it gives one.
12 func TestMulPow10AsStrconv(t *testing.T) {
13 r := rand.New(rand.NewSource(4))
14 decided := 0
15 const n = 1000000
16 for i := 0; i < n; i++ {
17 var w uint64
18 switch i % 3 {
19 case 0:
20 w = r.Uint64() % 10000000000000000000 // up to 19 digits
21 case 1:
22 w = r.Uint64() >> uint(r.Intn(64))
23 default:
24 w = uint64(r.Int63n(100000000000000000)) // 17 digits, as the shortest float64s
25 }
26 q := r.Intn(maxPow10Exp10-minPow10Exp10+1) + minPow10Exp10
27 if i%2 == 0 {
28 q = r.Intn(60) - 30 // the usual exponents
29 }
30 got, ok := mulPow10(w, q)
31 if !ok {
32 continue
33 }
34 decided++
35 want, err := strconv.ParseFloat(strconv.FormatUint(w, 10)+"e"+strconv.Itoa(q), 64)
36 if err != nil {
37 t.Fatalf("%de%d: mulPow10 gave %v, strconv failed: %v", w, q, got, err)
38 }
39 if math.Float64bits(got) != math.Float64bits(want) {
40 t.Fatalf("%de%d: got %v ( %x ), want %v ( %x )", w, q, got, math.Float64bits(got), want, math.Float64bits(want))
41 }
42 }
43 // nearly every number is decided by the table; the rest are left to strconv.
44 if decided < n*9/10 {
45 t.Fatalf("only %d of %d numbers were decided", decided, n)
46 }
47 t.Logf("%d of %d decided", decided, n)
48 }
49
50 // A number exactly half-way between two float64 values is never decided wrongly: the table gives none, or the even one.
51 func TestMulPow10HalfWay(t *testing.T) {
52 r := rand.New(rand.NewSource(5))
53 checked, decided := 0, 0
54 for i := 0; i < 10000; i++ {
55 // the half-way point between m*2^e and (m+1)*2^e, written as a decimal: (2m+1) * 2^(e-1).
56 // Its decimal has at most 19 digits for these exponents.
57 m := uint64(1)<<52 | r.Uint64()&(1<<52-1)
58 e := r.Intn(12) - 2
59 x := new(big.Float).SetPrec(2000).SetMantExp(new(big.Float).SetUint64(2*m+1), e-1)
60 s := x.Text('e', -1)
61 want, err := strconv.ParseFloat(s, 64)
62 if err != nil {
63 t.Fatal(err)
64 }
65 mant, exp, ok := splitDecimal(s)
66 if !ok {
67 continue
68 }
69 checked++
70 got, ok := mulPow10(mant, exp)
71 if !ok {
72 continue
73 }
74 decided++
75 if math.Float64bits(got) != math.Float64bits(want) {
76 t.Fatalf("%s: got %v, want %v", s, got, want)
77 }
78 }
79 if checked < 5000 {
80 t.Fatalf("only %d half-way points were checked", checked)
81 }
82 t.Logf("%d half-way points checked, %d decided by the table", checked, decided)
83 }
84
85 // splitDecimal returns the mantissa and the exponent of ten of a decimal of at most 19 digits.
86 func splitDecimal(s string) (uint64, int, bool) {
87 var mant uint64
88 digits, exp := 0, 0
89 i := 0
90 for ; i < len(s) && s[i] != 'e'; i++ {
91 switch c := s[i]; {
92 case c == '.':
93 exp = -(len(s[i+1:]) - len(s[indexByte(s, 'e'):]))
94 case c >= '0' && c <= '9':
95 mant = mant*10 + uint64(c-'0')
96 digits++
97 }
98 }
99 if digits > 19 {
100 return 0, 0, false
101 }
102 e, err := strconv.Atoi(s[i+1:])
103 if err != nil {
104 return 0, 0, false
105 }
106 return mant, exp + e, true
107 }
108
109 func indexByte(s string, c byte) int {
110 for i := 0; i < len(s); i++ {
111 if s[i] == c {
112 return i
113 }
114 }
115 return len(s)
116 }
117
118 // Every entry of the generated table is the truncated significand of its power of ten.
119 func TestPow10Table(t *testing.T) {
120 for q := minPow10Exp10; q <= maxPow10Exp10; q++ {
121 p := pow10Table[q-minPow10Exp10]
122 t128 := new(big.Int).Lsh(new(big.Int).SetUint64(p.hi), 64)
123 t128.Or(t128, new(big.Int).SetUint64(p.lo))
124 if t128.BitLen() != 128 {
125 t.Fatalf("1e%d: the significand has %d bits", q, t128.BitLen())
126 }
127 // T * 2^exp2 <= 10^q < (T+1) * 2^exp2, compared as rationals.
128 pow := new(big.Rat).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(abs(q))), nil))
129 if q < 0 {
130 pow.Inv(pow)
131 }
132 scale := new(big.Rat).SetInt(new(big.Int).Lsh(big.NewInt(1), uint(abs(p.exp2))))
133 if p.exp2 < 0 {
134 scale.Inv(scale)
135 }
136 low := new(big.Rat).Mul(new(big.Rat).SetInt(t128), scale)
137 high := new(big.Rat).Mul(new(big.Rat).SetInt(new(big.Int).Add(t128, big.NewInt(1))), scale)
138 if low.Cmp(pow) > 0 || high.Cmp(pow) <= 0 {
139 t.Fatalf("1e%d is not in [T, T+1) * 2^%d", q, p.exp2)
140 }
141 }
142 }
143
144 func abs(x int) int {
145 if x < 0 {
146 return -x
147 }
148 return x
149 }