Repository navigation
Expand file tree
/
Copy pathasinh128_test.go
More file actions
350 lines (317 loc) · 12.2 KB
/
Copy pathasinh128_test.go
File metadata and controls
350 lines (317 loc) · 12.2 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package floats
import (
"math"
"math/rand/v2"
"testing"
)
func TestFloat128_Asinh(t *testing.T) {
t.Parallel()
tests := []struct {
x Float128
want string
}{
{exact128(0), "0"},
{exact128(0x1p-59), "1.734723475976807094411924481391906737411209404266863396438627452099126993751360531E-18"},
{exact128(0.25), "0.247466461547263452944781549788359289253766903098567696469117357944365179443666365"},
{exact128(0.5), "0.4812118250596034474977589134243684231351843343856605196610181688401638676082217744"},
{exact128(1), "0.8813735870195430252326093249797923090281603282616354107532956086533771842220260878"},
{exact128(21), "3.738236030261542702926345171286353186644971465768736202615459010981959402821114935"},
{exact128(0x1p59), "41.58883083359671856503392728749059408528232444614157925234079195342585455113665898"},
{exact128(-0), "-0"},
{exact128(-0.25), "-0.247466461547263452944781549788359289253766903098567696469117357944365179443666365"},
{exact128(-0.5), "-0.4812118250596034474977589134243684231351843343856605196610181688401638676082217744"},
{exact128(-1), "-0.8813735870195430252326093249797923090281603282616354107532956086533771842220260878"},
{exact128(-21), "-3.738236030261542702926345171286353186644971465768736202615459010981959402821114935"},
}
for _, tt := range tests {
got := tt.x.Asinh()
if !close128(got, tt.want) {
t.Errorf("Asinh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
strictTests := []struct {
x Float128
want Float128
}{
{exact128(0), exact128(0)},
{exact128(math.Copysign(0, -1)), exact128(math.Copysign(0, -1))},
// special cases
{exact128(math.Inf(1)), exact128(math.Inf(1))},
{exact128(math.Inf(-1)), exact128(math.Inf(-1))},
{exact128(math.NaN()), exact128(math.NaN())},
// tiny values: asinh(x) = x - x**3/6 + ... rounds to x
{Float128{0x0000_0000_0000_0000, 0x0000_0000_0000_0001}, Float128{0x0000_0000_0000_0000, 0x0000_0000_0000_0001}},
{exact128(-0x1p-16382), exact128(-0x1p-16382)},
{exact128(0x1p-57), exact128(0x1p-57)},
{Float128{0x3fc6_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff}, Float128{0x3fc6_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff}}, // 2**-56 - 2**-169: x**3/6 = 0.33 half ulp
{exact128(0x1p-56), exact128(0x1p-56)}, // x**3/6 = 0.33 half ulp
{exact128(-0x1p-56), exact128(-0x1p-56)},
// the largest finite value: log(2x) is computed without overflow
{Float128{0x7ffe_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff}, Float128{0x400c_62e9_bb80_635d, 0x81d3_6125_b64d_a4a6}},
{Float128{0xfffe_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff}, Float128{0xc00c_62e9_bb80_635d, 0x81d3_6125_b64d_a4a6}},
}
for _, tt := range strictTests {
got := tt.x.Asinh()
if !eq128(got, tt.want) {
t.Errorf("Asinh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
}
// TestFloat128_AsinhAccuracy checks that the error of Asinh is less than 1 ulp,
// and that almost all results are correctly rounded.
// The test data is generated by scripts/gen_asinh_testdata.py.
func TestFloat128_AsinhAccuracy(t *testing.T) {
t.Parallel()
testFloat128Accuracy(t, "testdata/asinh128.txt", "Asinh", Float128.Asinh)
}
// TestFloat128_AsinhFloat256 compares Asinh with the result calculated in Float256 and rounded to Float128.
// The error of the Float256 result is about 2**-130 times smaller than the ulp of Float128,
// so they must be the same except for extremely rare cases.
func TestFloat128_AsinhFloat256(t *testing.T) {
t.Parallel()
rnd := rand.New(rand.NewPCG(1, 2))
one := Float256(uvone256)
for i := range 20000 {
var exp int
switch i % 4 {
case 0:
exp = rnd.IntN(16000) - 8000
case 1:
exp = rnd.IntN(130) - 70
default:
exp = rnd.IntN(30) - 5 + 55*(i/4%2) // around 1 and 2**60
}
x := Float128{rnd.Uint64(), rnd.Uint64()}
x[0] = x[0]&^(0x7fff<<48) | uint64(exp+16383)<<48
got := x.Asinh()
y := x.Float256()
abs := y
if x.Signbit() {
abs = y.Neg()
}
want := abs.Add(abs.Mul(abs).Add(one).Sqrt()).Log().Float128()
if exp < -56 {
want = abs.Float128() // log(1+x) loses the precision. asinh(x) rounds to x.
}
if x.Signbit() {
want = want.Neg()
}
if !eq128(got, want) {
t.Errorf("Asinh(%v) = %v; want %v", x, got, want)
}
}
}
func BenchmarkFloat128_Asinh(b *testing.B) {
benchFloat128(b, Float128.Asinh, []struct {
name string
x Float128
}{
{"subnormal", Float128{0, 1}}, // returns x
{"tiny", exact128(0x1p-60)}, // returns x
{"small", exact128(0.5)}, // |x| < 1
{"medium", exact128(1.5)}, // 1 <= |x| < 2
{"large", exact128(21)}, // 2 <= |x| < 2**60
{"negative", exact128(-10.3)}, // asinh(-x) = -asinh(x)
{"huge", exact128(1e300)}, // log(2x)
})
}
func TestFloat128_Acosh(t *testing.T) {
t.Parallel()
tests := []struct {
x Float128
want string
}{
{exact128(1), "0"},
{exact128(1.5), "0.9624236501192068949955178268487368462703686687713210393220363376803277352164435488"},
{exact128(21), "3.737102242198923900976805054113159386884204741143073889422198983717266311856623625"},
{exact128(0x1p59), "41.58883083359671856503392728749059408377769167708905124214080918578138008352876707"},
}
for _, tt := range tests {
got := tt.x.Acosh()
if !close128(got, tt.want) {
t.Errorf("Acosh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
strictTests := []struct {
x Float128
want Float128
}{
// special cases
{exact128(math.Inf(1)), exact128(math.Inf(1))},
{exact128(math.Inf(-1)), exact128(math.NaN())},
{exact128(math.NaN()), exact128(math.NaN())},
// x < 1
{exact128(0), exact128(math.NaN())},
{exact128(math.Copysign(0, -1)), exact128(math.NaN())},
{exact128(0.5), exact128(math.NaN())},
{Float128{0x3ffe_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff}, exact128(math.NaN())}, // 1 - 2**-113
{exact128(-1), exact128(math.NaN())},
{exact128(-2), exact128(math.NaN())},
// x is close to 1
{exact128(1), exact128(0)},
{Float128{0x3fff_0000_0000_0000, 0x0000_0000_0000_0001}, Float128{0x3fc7_6a09_e667_f3bc, 0xc908_b2fb_1366_ea95}}, // 1 + 2**-112
{Float128{0x3fff_0000_0000_0000, 0x0000_0000_0000_0002}, Float128{0x3fc8_0000_0000_0000, 0x0000_0000_0000_0000}}, // 1 + 2**-111
{Float128{0x3fff_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff}, Float128{0x3fff_5124_2719_8043, 0x49be_684b_d018_8d52}}, // 2 - 2**-112
// the largest finite value: log(2x) is computed without overflow
{Float128{0x7ffe_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff}, Float128{0x400c_62e9_bb80_635d, 0x81d3_6125_b64d_a4a6}},
}
for _, tt := range strictTests {
got := tt.x.Acosh()
if !eq128(got, tt.want) {
t.Errorf("Acosh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
}
// TestFloat128_AcoshAccuracy checks that the error of Acosh is less than 1 ulp,
// and that almost all results are correctly rounded.
// The test data is generated by scripts/gen_acosh_testdata.py.
func TestFloat128_AcoshAccuracy(t *testing.T) {
t.Parallel()
testFloat128Accuracy(t, "testdata/acosh128.txt", "Acosh", Float128.Acosh)
}
func BenchmarkFloat128_Acosh(b *testing.B) {
benchFloat128(b, Float128.Acosh, []struct {
name string
x Float128
}{
{"near1", Float128{0x3fff_0000_0000_0000, 1}}, // 1 + 2**-112
{"small", exact128(1.001)}, // x-1 is small
{"medium", exact128(1.5)}, // 1 < x <= 2
{"large", exact128(21)}, // 2 < x < 2**60
{"huge", exact128(1e300)}, // log(2x)
})
}
// TestFloat128_AcoshFloat256 compares Acosh with the result calculated in Float256 and rounded to Float128.
// The error of the Float256 result is about 2**-130 times smaller than the ulp of Float128,
// so they must be the same except for extremely rare cases.
func TestFloat128_AcoshFloat256(t *testing.T) {
t.Parallel()
rnd := rand.New(rand.NewPCG(1, 2))
one := Float256(uvone256)
for i := range 20000 {
var exp int
switch i % 4 {
case 0:
exp = rnd.IntN(16000)
case 1:
exp = rnd.IntN(4)
default:
exp = rnd.IntN(30) + 55*(i/4%2) // around 1 and 2**60
}
x := Float128{rnd.Uint64(), rnd.Uint64()}
x[0] = x[0]&0x0000_ffff_ffff_ffff | uint64(exp+16383)<<48
if i%8 == 0 {
x = Float128{0x3fff_0000_0000_0000, x[1] & 0xffff} // close to 1
}
if x == Float128(uvone128) {
continue
}
got := x.Acosh()
y := x.Float256()
want := y.Add(y.Mul(y).Sub(one).Sqrt()).Log().Float128()
if !eq128(got, want) {
t.Errorf("Acosh(%v) = %v; want %v", x, got, want)
}
}
}
func TestFloat128_Atanh(t *testing.T) {
t.Parallel()
tests := []struct {
x Float128
want string
}{
{exact128(0x1p-59), "1.73472347597680709441192448139190674002133119146627320712274509580174601799546362E-18"},
{exact128(0.25), "0.2554128118829953416027570481518309674390553982228841350889767789183423472445243989"},
{exact128(0.5), "0.5493061443340548456976226184612628523237452789113747258673471668187471466093044834"},
{exact128(0.75), "0.9729550745276566525526763717215898648185423647909305942296950749687899313760346339"},
{exact128(-0x1p-59), "-1.73472347597680709441192448139190674002133119146627320712274509580174601799546362E-18"},
{exact128(-0.25), "-0.2554128118829953416027570481518309674390553982228841350889767789183423472445243989"},
{exact128(-0.5), "-0.5493061443340548456976226184612628523237452789113747258673471668187471466093044834"},
{exact128(-0.75), "-0.9729550745276566525526763717215898648185423647909305942296950749687899313760346339"},
}
for _, tt := range tests {
got := tt.x.Atanh()
if !close128(got, tt.want) {
t.Errorf("Atanh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
strictTests := []struct {
x Float128
want Float128
}{
// special cases
{exact128(2), exact128(math.NaN())},
{exact128(1), exact128(math.Inf(1))},
{exact128(0), exact128(0)},
{exact128(math.Copysign(0, -1)), exact128(math.Copysign(0, -1))},
{exact128(-1), exact128(math.Inf(-1))},
{exact128(-2), exact128(math.NaN())},
{exact128(math.NaN()), exact128(math.NaN())},
{exact128(math.Inf(1)), exact128(math.NaN())},
{exact128(math.Inf(-1)), exact128(math.NaN())},
{Float128{0x3fff_0000_0000_0000, 1}, exact128(math.NaN())}, // 1 + 2**-112
{Float128{0xbfff_0000_0000_0000, 1}, exact128(math.NaN())},
{Float128{0x3fff_0000_0000_0001, 0}, exact128(math.NaN())}, // 1 + 2**-48
{Float128{0xbfff_0001_0000_0000, 0}, exact128(math.NaN())}, // -(1 + 2**-112)
{Float128{0x3fc5_0000_0000_0000, 0}, Float128{0x3fc5_0000_0000_0000, 0}}, // 2**-58: atanh(x) rounds to x
{Float128{0xbfc5_0000_0000_0000, 0}, Float128{0xbfc5_0000_0000_0000, 0}},
{Float128{0x0000_0000_0000_0000, 1}, Float128{0x0000_0000_0000_0000, 1}}, // the smallest subnormal
{Float128{0x8000_0000_0000_0000, 1}, Float128{0x8000_0000_0000_0000, 1}},
}
for _, tt := range strictTests {
got := tt.x.Atanh()
if !eq128(got, tt.want) {
t.Errorf("Atanh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
}
func TestFloat128_AtanhAccuracy(t *testing.T) {
t.Parallel()
testFloat128Accuracy(t, "testdata/atanh128.txt", "Atanh", Float128.Atanh)
}
func BenchmarkFloat128_Atanh(b *testing.B) {
benchFloat128(b, Float128.Atanh, []struct {
name string
x Float128
}{
{"tiny", exact128(0x1p-30)},
{"small", exact128(0.001)},
{"medium", exact128(0.5)},
{"near1", exact128(0.999)},
{"nearest1", Float128{0x3ffe_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff}}, // 1 - 2**-113
})
}
// TestFloat128_AtanhFloat256 compares Atanh with the result calculated in Float256 and rounded to Float128.
// The error of the Float256 result is about 2**-130 times smaller than the ulp of Float128,
// so they must be the same except for extremely rare cases.
func TestFloat128_AtanhFloat256(t *testing.T) {
t.Parallel()
rnd := rand.New(rand.NewPCG(1, 2))
one := Float256(uvone256)
two := one.Add(one)
for i := range 20000 {
var exp int
switch i % 4 {
case 0:
exp = -rnd.IntN(58) - 1
case 1:
exp = -rnd.IntN(4) - 1
default:
exp = -rnd.IntN(8) - 1
}
x := Float128{rnd.Uint64(), rnd.Uint64()}
x[0] = x[0]&0x8000_ffff_ffff_ffff | uint64(exp+16383)<<48
if i%8 == 1 {
x = Float128{x[0]&0x8000_0000_0000_0000 | 0x3ffe_ffff_ffff_ffff, x[1] | 0xffff_ffff_ffff_0000} // close to 1
}
got := x.Atanh()
if got.IsNaN() {
t.Fatalf("Atanh(%v) = NaN", x)
}
y := x.Float256()
want := one.Add(y).Quo(one.Sub(y)).Log().Quo(two).Float128()
if !eq128(got, want) {
t.Errorf("Atanh(%v) = %v; want %v", x, got, want)
}
}
}