-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpow_test.go
211 lines (198 loc) · 4.68 KB
/
pow_test.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package pow_test
import (
"fmt"
"math"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Konstantin8105/binaryexpr"
"github.com/Konstantin8105/pow"
)
func Test(t *testing.T) {
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(info.Name(), ".go") {
return nil
}
return binaryexpr.Test(path)
})
if err != nil {
t.Error(err)
}
}
func TestE2(t *testing.T) {
tcs := []struct {
x float64
result float64
}{
{1.0, 1.0},
{2.0, 4.0},
{3.0, 9.0},
{math.NaN(), math.NaN()},
{math.Inf(1), math.Inf(1)},
{math.Inf(-1), math.Inf(1)},
}
for i := range tcs {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
a := pow.E2(tcs[i].x)
e := math.Pow(tcs[i].x, 2.0)
if a != e && math.IsNaN(a) != math.IsNaN(e) {
t.Errorf("got %14e , want %.14e", a, e)
}
})
}
}
func TestE3(t *testing.T) {
tcs := []struct {
x float64
result float64
}{
{1.0, 1.0},
{2.0, 8.0},
{3.0, 27.0},
{math.NaN(), math.NaN()},
{math.Inf(1), math.Inf(1)},
{math.Inf(-1), math.Inf(-1)},
}
for i := range tcs {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
a := pow.E3(tcs[i].x)
e := math.Pow(tcs[i].x, 3.0)
if a != e && math.IsNaN(a) != math.IsNaN(e) {
t.Errorf("got %14e , want %.14e", a, e)
}
})
}
}
func TestE4(t *testing.T) {
tcs := []struct {
x float64
result float64
}{
{1.0, 1.0},
{2.0, 16.0},
{3.0, 81.0},
{math.NaN(), math.NaN()},
{math.Inf(1), math.Inf(1)},
{math.Inf(-1), math.Inf(-1)},
}
for i := range tcs {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
a := pow.E4(tcs[i].x)
e := math.Pow(tcs[i].x, 4.0)
if a != e && math.IsNaN(a) != math.IsNaN(e) {
t.Errorf("got %14e , want %.14e", a, e)
}
})
}
}
func TestEn(t *testing.T) {
tcs := []struct {
x float64
e int
}{
{1.0, 1.0},
{2.0, 8.0},
{3.0, 27.0},
{1.0, -1.0},
{2.0, -8.0},
{3.0, -27.0},
}
for i := range tcs {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
a := pow.En(tcs[i].x, tcs[i].e)
e := math.Pow(tcs[i].x, float64(tcs[i].e))
if a != e && math.IsNaN(a) != math.IsNaN(e) {
t.Errorf("got %14e , want %.14e", a, e)
}
})
}
}
func Example() {
x := 2.0
r2 := pow.E2(x) // math.Pow(x, 2.0)
r3 := pow.E3(x) // math.Pow(x, 3.0)
fmt.Fprintf(os.Stdout, "%.4f\n", r2)
fmt.Fprintf(os.Stdout, "%.4f\n", r3)
// Output:
// 4.0000
// 8.0000
}
// cpu: Intel(R) Xeon(R) CPU X5550 @ 2.67GHz
// Benchmark/math.Pow2-16 35098486 34.05 ns/op 0 B/op 0 allocs/op
// Benchmark/pow.E2-16 1000000000 1.028 ns/op 0 B/op 0 allocs/op
// Benchmark/pow.En2-16 142912671 8.224 ns/op 0 B/op 0 allocs/op
// Benchmark/math.Pow3-16 35733325 35.76 ns/op 0 B/op 0 allocs/op
// Benchmark/pow.E3-16 1000000000 1.056 ns/op 0 B/op 0 allocs/op
// Benchmark/pow.En3-16 126317251 9.516 ns/op 0 B/op 0 allocs/op
// Benchmark/math.Pow4-16 33246009 36.10 ns/op 0 B/op 0 allocs/op
// Benchmark/pow.E4-16 1000000000 1.021 ns/op 0 B/op 0 allocs/op
// Benchmark/pow.En4-16 126656416 9.524 ns/op 0 B/op 0 allocs/op
// Benchmark/math.Pow(x,_51)-16 27771769 43.08 ns/op 0 B/op 0 allocs/op
// Benchmark/pow.En(x,_51)-16 48447409 24.23 ns/op 0 B/op 0 allocs/op
func Benchmark(b *testing.B) {
x := math.Pi
var y float64
b.Run("math.Pow2", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = math.Pow(x, 2.0)
}
})
b.Run("pow.E2", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = pow.E2(x)
}
})
b.Run("pow.En2", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = pow.En(x, 2)
}
})
b.Run("math.Pow3", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = math.Pow(x, 3.0)
}
})
b.Run("pow.E3", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = pow.E3(x)
}
})
b.Run("pow.En3", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = pow.En(x, 3)
}
})
b.Run("math.Pow4", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = math.Pow(x, 4.0)
}
})
b.Run("pow.E4", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = pow.E4(x)
}
})
b.Run("pow.En4", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = pow.En(x, 4)
}
})
b.Run("math.Pow(x, 51)", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = math.Pow(x, 51.0)
}
})
b.Run("pow.En(x, 51)", func(b *testing.B) {
for i := 0; i < b.N; i++ {
y = pow.En(x, 51)
}
})
_ = y
}