File: scalar_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (369 lines) | stat: -rw-r--r-- 12,164 bytes parent folder | download
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright ©2013 The Gonum Authors. All rights reserved.
// Use of this code is governed by a BSD-style
// license that can be found in the LICENSE file.

package scalar

import (
	"math"
	"testing"
)

func TestEqualsRelative(t *testing.T) {
	t.Parallel()
	equalityTests := []struct {
		a, b  float64
		tol   float64
		equal bool
	}{
		{1000000, 1000001, 0, true},
		{1000001, 1000000, 0, true},
		{10000, 10001, 0, false},
		{10001, 10000, 0, false},
		{-1000000, -1000001, 0, true},
		{-1000001, -1000000, 0, true},
		{-10000, -10001, 0, false},
		{-10001, -10000, 0, false},
		{1.0000001, 1.0000002, 0, true},
		{1.0000002, 1.0000001, 0, true},
		{1.0002, 1.0001, 0, false},
		{1.0001, 1.0002, 0, false},
		{-1.000001, -1.000002, 0, true},
		{-1.000002, -1.000001, 0, true},
		{-1.0001, -1.0002, 0, false},
		{-1.0002, -1.0001, 0, false},
		{0.000000001000001, 0.000000001000002, 0, true},
		{0.000000001000002, 0.000000001000001, 0, true},
		{0.000000000001002, 0.000000000001001, 0, false},
		{0.000000000001001, 0.000000000001002, 0, false},
		{-0.000000001000001, -0.000000001000002, 0, true},
		{-0.000000001000002, -0.000000001000001, 0, true},
		{-0.000000000001002, -0.000000000001001, 0, false},
		{-0.000000000001001, -0.000000000001002, 0, false},
		{0, 0, 0, true},
		{0, -0, 0, true},
		{-0, -0, 0, true},
		{0.00000001, 0, 0, false},
		{0, 0.00000001, 0, false},
		{-0.00000001, 0, 0, false},
		{0, -0.00000001, 0, false},
		{0, 1e-310, 0.01, true},
		{1e-310, 0, 0.01, true},
		{1e-310, 0, 0.000001, false},
		{0, 1e-310, 0.000001, false},
		{0, -1e-310, 0.1, true},
		{-1e-310, 0, 0.1, true},
		{-1e-310, 0, 0.00000001, false},
		{0, -1e-310, 0.00000001, false},
		{math.Inf(1), math.Inf(1), 0, true},
		{math.Inf(-1), math.Inf(-1), 0, true},
		{math.Inf(-1), math.Inf(1), 0, false},
		{math.Inf(1), math.MaxFloat64, 0, false},
		{math.Inf(-1), -math.MaxFloat64, 0, false},
		{math.NaN(), math.NaN(), 0, false},
		{math.NaN(), 0, 0, false},
		{-0, math.NaN(), 0, false},
		{math.NaN(), -0, 0, false},
		{0, math.NaN(), 0, false},
		{math.NaN(), math.Inf(1), 0, false},
		{math.Inf(1), math.NaN(), 0, false},
		{math.NaN(), math.Inf(-1), 0, false},
		{math.Inf(-1), math.NaN(), 0, false},
		{math.NaN(), math.MaxFloat64, 0, false},
		{math.MaxFloat64, math.NaN(), 0, false},
		{math.NaN(), -math.MaxFloat64, 0, false},
		{-math.MaxFloat64, math.NaN(), 0, false},
		{math.NaN(), math.SmallestNonzeroFloat64, 0, false},
		{math.SmallestNonzeroFloat64, math.NaN(), 0, false},
		{math.NaN(), -math.SmallestNonzeroFloat64, 0, false},
		{-math.SmallestNonzeroFloat64, math.NaN(), 0, false},
		{1.000000001, -1.0, 0, false},
		{-1.0, 1.000000001, 0, false},
		{-1.000000001, 1.0, 0, false},
		{1.0, -1.000000001, 0, false},
		{10 * math.SmallestNonzeroFloat64, 10 * -math.SmallestNonzeroFloat64, 0, true},
		{1e11 * math.SmallestNonzeroFloat64, 1e11 * -math.SmallestNonzeroFloat64, 0, false},
		{math.SmallestNonzeroFloat64, -math.SmallestNonzeroFloat64, 0, true},
		{-math.SmallestNonzeroFloat64, math.SmallestNonzeroFloat64, 0, true},
		{math.SmallestNonzeroFloat64, 0, 0, true},
		{0, math.SmallestNonzeroFloat64, 0, true},
		{-math.SmallestNonzeroFloat64, 0, 0, true},
		{0, -math.SmallestNonzeroFloat64, 0, true},
		{0.000000001, -math.SmallestNonzeroFloat64, 0, false},
		{0.000000001, math.SmallestNonzeroFloat64, 0, false},
		{math.SmallestNonzeroFloat64, 0.000000001, 0, false},
		{-math.SmallestNonzeroFloat64, 0.000000001, 0, false},
	}
	for _, ts := range equalityTests {
		if ts.tol == 0 {
			ts.tol = 1e-5
		}
		if equal := EqualWithinRel(ts.a, ts.b, ts.tol); equal != ts.equal {
			t.Errorf("Relative equality of %g and %g with tolerance %g returned: %v. Expected: %v",
				ts.a, ts.b, ts.tol, equal, ts.equal)
		}
	}
}

func TestEqualsULP(t *testing.T) {
	t.Parallel()
	if f := 67329.242; !EqualWithinULP(f, nextAfterN(f, math.Inf(1), 10), 10) {
		t.Errorf("Equal values returned as unequal")
	}
	if f := 67329.242; EqualWithinULP(f, nextAfterN(f, math.Inf(1), 5), 1) {
		t.Errorf("Unequal values returned as equal")
	}
	if f := 67329.242; EqualWithinULP(nextAfterN(f, math.Inf(1), 5), f, 1) {
		t.Errorf("Unequal values returned as equal")
	}
	if f := nextAfterN(0, math.Inf(1), 2); !EqualWithinULP(f, nextAfterN(f, math.Inf(-1), 5), 10) {
		t.Errorf("Equal values returned as unequal")
	}
	if !EqualWithinULP(67329.242, 67329.242, 10) {
		t.Errorf("Equal float64s not returned as equal")
	}
	if EqualWithinULP(1, math.NaN(), 10) {
		t.Errorf("NaN returned as equal")
	}
}

func nextAfterN(x, y float64, n int) float64 {
	for i := 0; i < n; i++ {
		x = math.Nextafter(x, y)
	}
	return x
}

func TestNaNWith(t *testing.T) {
	t.Parallel()
	tests := []struct {
		payload uint64
		bits    uint64
	}{
		{0, math.Float64bits(0 / func() float64 { return 0 }())}, // Hide the division by zero from the compiler.
		{1, math.Float64bits(math.NaN())},
		{1954, 0x7ff80000000007a2}, // R NA.
	}

	for _, test := range tests {
		nan := NaNWith(test.payload)
		if !math.IsNaN(nan) {
			t.Errorf("expected NaN value, got:%f", nan)
		}

		bits := math.Float64bits(nan)

		// Strip sign bit.
		const sign = 1 << 63
		bits &^= sign
		test.bits &^= sign

		if bits != test.bits {
			t.Errorf("expected NaN bit representation: got:%x want:%x", bits, test.bits)
		}
	}
}

func TestNaNPayload(t *testing.T) {
	t.Parallel()
	tests := []struct {
		f       float64
		payload uint64
		ok      bool
	}{
		{0 / func() float64 { return 0 }(), 0, true}, // Hide the division by zero from the compiler.

		// The following two line are written explicitly to defend against potential changes to math.Copysign.
		{math.Float64frombits(math.Float64bits(math.NaN()) | (1 << 63)), 1, true},  // math.Copysign(math.NaN(), -1)
		{math.Float64frombits(math.Float64bits(math.NaN()) &^ (1 << 63)), 1, true}, // math.Copysign(math.NaN(), 1)

		{NaNWith(1954), 1954, true}, // R NA.

		{math.Copysign(0, -1), 0, false},
		{0, 0, false},
		{math.Inf(-1), 0, false},
		{math.Inf(1), 0, false},

		{math.Float64frombits(0x7ff0000000000001), 0, false}, // Signalling NaN.
	}

	for _, test := range tests {
		payload, ok := NaNPayload(test.f)
		if payload != test.payload {
			t.Errorf("expected NaN payload: got:%x want:%x", payload, test.payload)
		}
		if ok != test.ok {
			t.Errorf("expected NaN status: got:%t want:%t", ok, test.ok)
		}
	}
}

func TestRound(t *testing.T) {
	t.Parallel()
	for _, test := range []struct {
		x    float64
		prec int
		want float64
	}{
		{x: 0, prec: 1, want: 0},
		{x: math.Inf(1), prec: 1, want: math.Inf(1)},
		{x: math.Inf(-1), prec: 1, want: math.Inf(-1)},
		{x: math.NaN(), prec: 1, want: math.NaN()},
		{x: func() float64 { var f float64; return -f }(), prec: 1, want: 0},
		{x: math.MaxFloat64 / 2, prec: 1, want: math.MaxFloat64 / 2},
		{x: 1 << 64, prec: 1, want: 1 << 64},
		{x: 454.4445, prec: 3, want: 454.445},
		{x: 454.44445, prec: 4, want: 454.4445},
		{x: 0.42499, prec: 4, want: 0.425},
		{x: 0.42599, prec: 4, want: 0.426},
		{x: 0.424999999999993, prec: 2, want: 0.42},
		{x: 0.425, prec: 2, want: 0.43},
		{x: 0.425000000000001, prec: 2, want: 0.43},
		{x: 123.4244999999999, prec: 3, want: 123.424},
		{x: 123.4245, prec: 3, want: 123.425},
		{x: 123.4245000000001, prec: 3, want: 123.425},
		{x: -0.49999999999999994, prec: 0, want: 0},
		{x: 0.49999999999999994, prec: 0, want: 0},

		{x: 454.45, prec: 0, want: 454},
		{x: 454.45, prec: 1, want: 454.5},
		{x: 454.45, prec: 2, want: 454.45},
		{x: 454.45, prec: 3, want: 454.45},
		{x: 454.445, prec: 0, want: 454},
		{x: 454.445, prec: 1, want: 454.4},
		{x: 454.445, prec: 2, want: 454.45},
		{x: 454.445, prec: 3, want: 454.445},
		{x: 454.445, prec: 4, want: 454.445},
		{x: 454.55, prec: 0, want: 455},
		{x: 454.55, prec: 1, want: 454.6},
		{x: 454.55, prec: 2, want: 454.55},
		{x: 454.55, prec: 3, want: 454.55},
		{x: 454.455, prec: 0, want: 454},
		{x: 454.455, prec: 1, want: 454.5},
		{x: 454.455, prec: 2, want: 454.46},
		{x: 454.455, prec: 3, want: 454.455},
		{x: 454.455, prec: 4, want: 454.455},

		// Negative precision.
		{x: math.Inf(1), prec: -1, want: math.Inf(1)},
		{x: math.Inf(-1), prec: -1, want: math.Inf(-1)},
		{x: math.NaN(), prec: -1, want: math.NaN()},
		{x: func() float64 { var f float64; return -f }(), prec: -1, want: 0},
		{x: 454.45, prec: -1, want: 450},
		{x: 454.45, prec: -2, want: 500},
		{x: 500, prec: -3, want: 1000},
		{x: 500, prec: -4, want: 0},
		{x: 1500, prec: -3, want: 2000},
		{x: 1500, prec: -4, want: 0},
	} {
		for _, sign := range []float64{1, -1} {
			got := Round(sign*test.x, test.prec)
			want := sign * test.want
			if want == 0 {
				want = 0
			}
			if (got != want || math.Signbit(got) != math.Signbit(want)) && !(math.IsNaN(got) && math.IsNaN(want)) {
				t.Errorf("unexpected result for Round(%g, %d): got: %g, want: %g", sign*test.x, test.prec, got, want)
			}
		}
	}
}

func TestRoundEven(t *testing.T) {
	t.Parallel()
	for _, test := range []struct {
		x    float64
		prec int
		want float64
	}{
		{x: 0, prec: 1, want: 0},
		{x: math.Inf(1), prec: 1, want: math.Inf(1)},
		{x: math.Inf(-1), prec: 1, want: math.Inf(-1)},
		{x: math.NaN(), prec: 1, want: math.NaN()},
		{x: func() float64 { var f float64; return -f }(), prec: 1, want: 0},
		{x: math.MaxFloat64 / 2, prec: 1, want: math.MaxFloat64 / 2},
		{x: 1 << 64, prec: 1, want: 1 << 64},
		{x: 454.4445, prec: 3, want: 454.444},
		{x: 454.44445, prec: 4, want: 454.4444},
		{x: 0.42499, prec: 4, want: 0.425},
		{x: 0.42599, prec: 4, want: 0.426},
		{x: 0.424999999999993, prec: 2, want: 0.42},
		{x: 0.425, prec: 2, want: 0.42},
		{x: 0.425000000000001, prec: 2, want: 0.43},
		{x: 123.4244999999999, prec: 3, want: 123.424},
		{x: 123.4245, prec: 3, want: 123.424},
		{x: 123.4245000000001, prec: 3, want: 123.425},
		{x: -0.49999999999999994, prec: 0, want: 0},
		{x: 0.49999999999999994, prec: 0, want: 0},

		{x: 454.45, prec: 0, want: 454},
		{x: 454.45, prec: 1, want: 454.4},
		{x: 454.45, prec: 2, want: 454.45},
		{x: 454.45, prec: 3, want: 454.45},
		{x: 454.445, prec: 0, want: 454},
		{x: 454.445, prec: 1, want: 454.4},
		{x: 454.445, prec: 2, want: 454.44},
		{x: 454.445, prec: 3, want: 454.445},
		{x: 454.445, prec: 4, want: 454.445},
		{x: 454.55, prec: 0, want: 455},
		{x: 454.55, prec: 1, want: 454.6},
		{x: 454.55, prec: 2, want: 454.55},
		{x: 454.55, prec: 3, want: 454.55},
		{x: 454.455, prec: 0, want: 454},
		{x: 454.455, prec: 1, want: 454.5},
		{x: 454.455, prec: 2, want: 454.46},
		{x: 454.455, prec: 3, want: 454.455},
		{x: 454.455, prec: 4, want: 454.455},

		// Negative precision.
		{x: math.Inf(1), prec: -1, want: math.Inf(1)},
		{x: math.Inf(-1), prec: -1, want: math.Inf(-1)},
		{x: math.NaN(), prec: -1, want: math.NaN()},
		{x: func() float64 { var f float64; return -f }(), prec: -1, want: 0},
		{x: 454.45, prec: -1, want: 450},
		{x: 454.45, prec: -2, want: 500},
		{x: 500, prec: -3, want: 0},
		{x: 500, prec: -4, want: 0},
		{x: 1500, prec: -3, want: 2000},
		{x: 1500, prec: -4, want: 0},
	} {
		for _, sign := range []float64{1, -1} {
			got := RoundEven(sign*test.x, test.prec)
			want := sign * test.want
			if want == 0 {
				want = 0
			}
			if (got != want || math.Signbit(got) != math.Signbit(want)) && !(math.IsNaN(got) && math.IsNaN(want)) {
				t.Errorf("unexpected result for RoundEven(%g, %d): got: %g, want: %g", sign*test.x, test.prec, got, want)
			}
		}
	}
}

func TestSame(t *testing.T) {
	t.Parallel()
	for _, test := range []struct {
		a, b float64
		want bool
	}{
		{a: 0, b: 0, want: true},
		{a: 1, b: 1, want: true},
		{a: -1, b: 1, want: false},
		{a: 0, b: 1, want: false},
		{a: 1, b: 0, want: false},
		{a: -1, b: 1, want: false},
		{a: math.NaN(), b: math.NaN(), want: true},
		{a: 1, b: math.NaN(), want: false},
		{a: math.Inf(1), b: math.NaN(), want: false},
		{a: math.NaN(), b: math.Inf(1), want: false},
		{a: math.NaN(), b: 1, want: false},
		{a: math.Inf(1), b: math.Inf(1), want: true},
		{a: math.Inf(-1), b: math.Inf(1), want: false},
		{a: math.Inf(1), b: math.Inf(-1), want: false},
	} {
		got := Same(test.a, test.b)
		if got != test.want {
			t.Errorf("unexpected results for a=%f b=%f: got:%t want:%t", test.a, test.b, got, test.want)
		}
	}
}