Repository navigation
Expand file tree
/
Copy pathasinh256_test.go
More file actions
224 lines (199 loc) · 7.97 KB
/
Copy pathasinh256_test.go
File metadata and controls
224 lines (199 loc) · 7.97 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
package floats
import (
"math"
"testing"
)
func TestFloat256_Asinh(t *testing.T) {
t.Parallel()
tests := []struct {
x Float256
want string
}{
{exact256(0), "0"},
{exact256(0x1p-171), "3.340955887615244557675670583939352348518996100131310872452421865200498301233912948E-52"},
{exact256(0.25), "0.247466461547263452944781549788359289253766903098567696469117357944365179443666365"},
{exact256(0.5), "0.4812118250596034474977589134243684231351843343856605196610181688401638676082217744"},
{exact256(1), "0.8813735870195430252326093249797923090281603282616354107532956086533771842220260878"},
{exact256(21), "3.738236030261542702926345171286353186644971465768736202615459010981959402821114935"},
{exact256(0x1p171), "119.2213150563105932197639248908063697089860231099639037087569616328637029787874911"},
{exact256(-0), "-0"},
{exact256(-0.25), "-0.247466461547263452944781549788359289253766903098567696469117357944365179443666365"},
{exact256(-0.5), "-0.4812118250596034474977589134243684231351843343856605196610181688401638676082217744"},
{exact256(-1), "-0.8813735870195430252326093249797923090281603282616354107532956086533771842220260878"},
{exact256(-21), "-3.738236030261542702926345171286353186644971465768736202615459010981959402821114935"},
}
for _, tt := range tests {
got := tt.x.Asinh()
if !close256(got, tt.want) {
t.Errorf("Asinh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
strictTests := []struct {
x Float256
want Float256
}{
{exact256(0), exact256(0)},
{exact256(math.Copysign(0, -1)), exact256(math.Copysign(0, -1))},
// special cases
{exact256(math.Inf(1)), exact256(math.Inf(1))},
{exact256(math.Inf(-1)), exact256(math.Inf(-1))},
{exact256(math.NaN()), exact256(math.NaN())},
// asinh(x) rounds to x for tiny x, including the smallest subnormal numbers
{Float256{0, 0, 0, 1}, Float256{0, 0, 0, 1}},
{Float256{signMask256[0], 0, 0, 1}, Float256{signMask256[0], 0, 0, 1}},
{exact256(0x1p-119), exact256(0x1p-119)},
{exact256(0x1p-118), exact256(0x1p-118)},
{exact256(0x1p-118).Nextafter(NewFloat256Inf(-1)), exact256(0x1p-118).Nextafter(NewFloat256Inf(-1))},
{exact256(-0x1p-118), exact256(-0x1p-118)},
}
for _, tt := range strictTests {
got := tt.x.Asinh()
if !eq256(got, tt.want) {
t.Errorf("Asinh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
}
func TestFloat256_AsinhAccuracy(t *testing.T) {
t.Parallel()
testFloat256Accuracy(t, "testdata/asinh256.txt", "Asinh", Float256.Asinh)
}
func BenchmarkFloat256_Asinh(b *testing.B) {
benchFloat256(b, Float256.Asinh, []struct {
name string
x Float256
}{
{"tiny", exact256(0x1p-120)}, // returns x
{"small", exact256(0x1p-20)}, // t = a + a²/(1+sqrt(1+a²)) < 2**-8
{"medium", exact256(0.5)}, // |a| < 1
{"negative", exact256(-1.5)}, // asinh(-a) = -asinh(a)
{"large", exact256(21)}, // 1 <= |a| < 2**128
{"huge", exact256(1e300)}, // log(2a)
})
}
func TestFloat256_Acosh(t *testing.T) {
t.Parallel()
tests := []struct {
x Float256
want string
}{
{exact256(1), "0"},
{exact256(1.5), "0.9624236501192068949955178268487368462703686687713210393220363376803277352164435488"},
{exact256(21), "3.737102242198923900976805054113159386884204741143073889422198983717266311856623625"},
{exact256(0x1p59), "41.58883083359671856503392728749059408377769167708905124214080918578138008352876707"},
}
for _, tt := range tests {
got := tt.x.Acosh()
if !close256(got, tt.want) {
t.Errorf("Acosh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
strictTests := []struct {
x Float256
want Float256
}{
// special cases
{exact256(math.Inf(1)), exact256(math.Inf(1))},
{exact256(math.Inf(-1)), exact256(math.NaN())},
{exact256(math.NaN()), exact256(math.NaN())},
{exact256(1), exact256(0)},
{exact256(0), exact256(math.NaN())},
{exact256(math.Copysign(0, -1)), exact256(math.NaN())},
{exact256(-1), exact256(math.NaN())},
{exact256(0.5), exact256(math.NaN())},
{exact256(1).Nextafter(exact256(0)), exact256(math.NaN())}, // the largest number less than 1
}
for _, tt := range strictTests {
got := tt.x.Acosh()
if !eq256(got, tt.want) {
t.Errorf("Acosh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
}
func TestFloat256_AcoshAccuracy(t *testing.T) {
t.Parallel()
testFloat256Accuracy(t, "testdata/acosh256.txt", "Acosh", Float256.Acosh)
}
func BenchmarkFloat256_Acosh(b *testing.B) {
benchFloat256(b, Float256.Acosh, []struct {
name string
x Float256
}{
{"near1", exact256(1).Nextafter(exact256(2))}, // 1 + 2**-236
{"small", exact256(1.001)}, // t = (a-1) + sqrt(a²-1) < 2**-8
{"medium", exact256(1.5)}, // 1 < a < 2
{"large", exact256(21)}, // 2 <= a < 2**128
{"huge", exact256(1e300)}, // log(2a)
})
}
func TestFloat256_Atanh(t *testing.T) {
t.Parallel()
tests := []struct {
x Float256
want string
}{
{exact256(0x1p-171), "3.340955887615244557675670583939352348518996100131310872452421865200498301233912948E-52"},
{exact256(0.25), "0.2554128118829953416027570481518309674390553982228841350889767789183423472445243989"},
{exact256(0.5), "0.5493061443340548456976226184612628523237452789113747258673471668187471466093044834"},
{exact256(0.75), "0.9729550745276566525526763717215898648185423647909305942296950749687899313760346339"},
{exact256(-0x1p-171), "-3.340955887615244557675670583939352348518996100131310872452421865200498301233912948E-52"},
{exact256(-0.25), "-0.2554128118829953416027570481518309674390553982228841350889767789183423472445243989"},
{exact256(-0.5), "-0.5493061443340548456976226184612628523237452789113747258673471668187471466093044834"},
{exact256(-0.75), "-0.9729550745276566525526763717215898648185423647909305942296950749687899313760346339"},
}
for _, tt := range tests {
got := tt.x.Atanh()
if !close256(got, tt.want) {
t.Errorf("Atanh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
strictTests := []struct {
x Float256
want Float256
}{
// special cases
{exact256(2), exact256(math.NaN())},
{exact256(1), exact256(math.Inf(1))},
{exact256(0), exact256(0)},
{exact256(math.Copysign(0, -1)), exact256(math.Copysign(0, -1))},
{exact256(-1), exact256(math.Inf(-1))},
{exact256(-2), exact256(math.NaN())},
{exact256(math.NaN()), exact256(math.NaN())},
{exact256(math.Inf(1)), exact256(math.NaN())},
{exact256(math.Inf(-1)), exact256(math.NaN())},
{exact256(1).Nextafter(exact256(2)), exact256(math.NaN())}, // 1 + 2**-236
{exact256(-1).Nextafter(exact256(-2)), exact256(math.NaN())}, // -(1 + 2**-236)
// atanh(x) rounds to x for tiny x, including the smallest subnormal numbers
{Float256{0, 0, 0, 1}, Float256{0, 0, 0, 1}},
{Float256{signMask256[0], 0, 0, 1}, Float256{signMask256[0], 0, 0, 1}},
{exact256(0x1p-120), exact256(0x1p-120)},
{exact256(0x1p-119), exact256(0x1p-119)},
{exact256(-0x1p-119), exact256(-0x1p-119)},
{exact256(0x1p-119).Nextafter(NewFloat256Inf(-1)), exact256(0x1p-119).Nextafter(NewFloat256Inf(-1))},
// just above the threshold, atanh(x) = x + x³/3 + ... is not rounded to x for the largest x
{exact256(0x1p-118).Nextafter(NewFloat256Inf(-1)), exact256(0x1p-118)},
{exact256(-0x1p-118).Nextafter(NewFloat256Inf(1)), exact256(-0x1p-118)},
}
for _, tt := range strictTests {
got := tt.x.Atanh()
if !eq256(got, tt.want) {
t.Errorf("Atanh(%v) = %v; want %v", tt.x, got, tt.want)
}
}
}
func TestFloat256_AtanhAccuracy(t *testing.T) {
t.Parallel()
testFloat256Accuracy(t, "testdata/atanh256.txt", "Atanh", Float256.Atanh)
}
func BenchmarkFloat256_Atanh(b *testing.B) {
benchFloat256(b, Float256.Atanh, []struct {
name string
x Float256
}{
{"tiny", exact256(0x1p-125)}, // returns x
{"small", exact256(0x1p-20)}, // series
{"medium", exact256(0.5)}, // logarithm
{"negative", exact256(-0.75)}, // atanh(-a) = -atanh(a)
{"near1", exact256(0.999)}, // logarithm
{"nearest1", exact256(1).Nextafter(exact256(0))}, // 1 - 2**-237
})
}