-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf256toa_test.go
More file actions
242 lines (215 loc) · 5.99 KB
/
f256toa_test.go
File metadata and controls
242 lines (215 loc) · 5.99 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
package floats
import (
"fmt"
"math"
"testing"
"github.com/shogo82148/ints"
)
func TestFloat256_Format(t *testing.T) {
tests := []struct {
format string
x Float256
want string
}{
// verb "%b"
{"%b", exact256(0), "0p-262378"},
// verb "%f"
{"%f", exact256(0.5), "0.5"},
{"%f", exact256(-0.5), "-0.5"},
{"%+f", exact256(0.5), "+0.5"},
{"%+f", exact256(-0.5), "-0.5"},
{"% f", exact256(0.5), " 0.5"},
{"% f", exact256(-0.5), "-0.5"},
{"%8f", exact256(0.5), " 0.5"},
{"%-8f", exact256(0.5), "0.5 "},
{"%.2f", exact256(0.5), "0.50"},
// verb "%e"
{"%.6e", exact256(0.5), "5.000000e-01"},
// verb "%g"
{"%g", exact256(0.5), "0.5"},
{"%.1g", exact256(0.25), "0.2"},
// verb "%x"
{"%x", exact256(0.5), "0x1p-01"},
{"%#x", exact256(0.5), "0x1p-01"},
{"%.1x", exact256(0.5), "0x1.0p-01"},
// verb "%X"
{"%X", exact256(0.5), "0X1P-01"},
{"%#X", exact256(0.5), "0X1P-01"},
{"%.1X", exact256(0.5), "0X1.0P-01"},
// verb "%v"
{"%v", exact256(0.5), "0.5"},
{"%v", exact256(math.NaN()), "NaN"},
}
for _, tt := range tests {
got := fmt.Sprintf(tt.format, tt.x)
if got != tt.want {
t.Errorf("expected %s, got %s", tt.want, got)
}
}
}
func TestFloat256_Text(t *testing.T) {
tests := []struct {
x Float256
fmt byte
prec int
s string
}{
/****** decimal formats ******/
{exact256(0x1p-24), 'f', 24, "0.000000059604644775390625"},
{exact256(0x2p-24), 'f', 24, "0.000000119209289550781250"},
{exact256(0x3p-24), 'f', 24, "0.000000178813934326171875"},
{exact256(0x4p-24), 'f', 24, "0.000000238418579101562500"},
{exact256(0x5p-24), 'f', 24, "0.000000298023223876953125"},
{exact256(0x6p-24), 'f', 24, "0.000000357627868652343750"},
{exact256(0x7p-24), 'f', 24, "0.000000417232513427734375"},
{exact256(0x8p-24), 'f', 24, "0.000000476837158203125000"},
{exact256(0x9p-24), 'f', 24, "0.000000536441802978515625"},
{exact256(0xap-24), 'f', 24, "0.000000596046447753906250"},
/****** binary exponent formats ******/
{exact256(0.0), 'b', -1, "0p-262378"},
{exact256(1.0), 'b', -1, "110427941548649020598956093796432407239217743554726184882600387580788736p-236"},
/******* alternate formats *******/
{
// largest normal number
Float256{
0x7fff_efff_ffff_ffff, 0xffff_ffff_ffff_ffff,
0xffff_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff,
}, 'g', -1,
"1.61132571748576047361957211845200501064402387454966951747637125049607183e+78913",
},
{
// smallest positive normal number
Float256{
0x0000_1000_0000_0000, 0x0000_0000_0000_0000,
0x0000_0000_0000_0000, 0x0000_0000_0000_0000,
}, 'g', -1,
"2.48242795146434978829932822291387172367768770607964686927095329791378756e-78913",
},
{
// largest subnormal number
Float256{
0x0000_0fff_ffff_ffff, 0xffff_ffff_ffff_ffff,
0xffff_ffff_ffff_ffff, 0xffff_ffff_ffff_ffff,
}, 'g', -1,
"2.48242795146434978829932822291387172367768770607964686927095329791378754e-78913",
},
{
// smallest positive subnormal number
Float256{
0x0000_0000_0000_0000, 0x0000_0000_0000_0000,
0x0000_0000_0000_0000, 0x0000_0000_0000_0001,
}, 'g', -1, "2e-78984",
},
/******* hexadecimal formats *******/
{exact256(0), 'x', -1, "0x0p+00"},
{exact256(math.Inf(1)), 'x', -1, "+Inf"},
{exact256(math.Inf(-1)), 'x', -1, "-Inf"},
{exact256(math.NaN()), 'x', -1, "NaN"},
{exact256(0x0.0p0), 'x', 0, "0x0p+00"},
{exact256(0x1.0p0), 'x', 0, "0x1p+00"},
{exact256(0x1.7p0), 'x', 0, "0x1p+00"},
{exact256(0x1.8p0), 'x', 0, "0x1p+01"},
{exact256(0x0.0p0), 'x', 1, "0x0.0p+00"},
{exact256(0x1.0p0), 'x', 1, "0x1.0p+00"},
{exact256(0x1.fp0), 'x', 1, "0x1.fp+00"},
{exact256(0x1.08p0), 'x', 1, "0x1.0p+00"},
{exact256(0x1.18p0), 'x', 1, "0x1.2p+00"},
{exact256(0x1p0), 'x', -1, "0x1p+00"},
{exact256(0x1.8p0), 'x', -1, "0x1.8p+00"},
{exact256(0x1.08p0), 'x', -1, "0x1.08p+00"},
{exact256(0x1.00cp0), 'x', -1, "0x1.00cp+00"},
}
for _, tt := range tests {
if got := tt.x.Text(tt.fmt, tt.prec); got != tt.s {
t.Errorf("Float256(%x).Text(%q, %d) = %q, want %q", ints.Uint256(tt.x), tt.fmt, tt.prec, got, tt.s)
}
}
}
func TestFloat256_MarshalJSON(t *testing.T) {
tests := []struct {
x Float256
want string
}{
{exact256(0), "0"},
{exact256(-0), "0"},
{exact256(1), "1"},
{exact256(-1), "-1"},
{exact256(0.5), "0.5"},
}
for _, tt := range tests {
got, err := tt.x.MarshalJSON()
if err != nil {
t.Errorf("unexpected error: %v", err)
continue
}
if string(got) != tt.want {
t.Errorf("expected %s, got %s", tt.want, got)
}
}
// JSON does not support NaN and Inf values.
var err error
_, err = exact256(math.NaN()).MarshalJSON()
if err == nil {
t.Errorf("expected error, got nil")
}
_, err = exact256(math.Inf(1)).MarshalJSON()
if err == nil {
t.Errorf("expected error, got nil")
}
_, err = exact256(math.Inf(-1)).MarshalJSON()
if err == nil {
t.Errorf("expected error, got nil")
}
}
func TestFloat256_MarshalText(t *testing.T) {
tests := []struct {
x Float256
want string
}{
{exact256(0), "0"},
{exact256(-0), "0"},
{exact256(1), "1"},
{exact256(-1), "-1"},
{exact256(0.5), "0.5"},
// special values
{exact256(math.Inf(1)), "+Inf"},
{exact256(math.Inf(-1)), "-Inf"},
{exact256(math.NaN()), "NaN"},
}
for _, tt := range tests {
got, err := tt.x.MarshalText()
if err != nil {
t.Errorf("unexpected error: %v", err)
continue
}
if string(got) != tt.want {
t.Errorf("expected %s, got %s", tt.want, got)
}
}
}
func TestFloat256_AppendText(t *testing.T) {
tests := []struct {
x Float256
want string
}{
{exact256(0), "0"},
{exact256(-0), "0"},
{exact256(1), "1"},
{exact256(-1), "-1"},
{exact256(0.5), "0.5"},
// special values
{exact256(math.Inf(1)), "+Inf"},
{exact256(math.Inf(-1)), "-Inf"},
{exact256(math.NaN()), "NaN"},
}
for _, tt := range tests {
got, err := tt.x.AppendText(nil)
if err != nil {
t.Errorf("unexpected error: %v", err)
continue
}
if string(got) != tt.want {
t.Errorf("expected %s, got %s", tt.want, got)
}
}
}