gonum.org/v1/gonum@v0.14.0/cmplxs/cscalar/cscalar_test.go (about) 1 // Copyright ©2013 The Gonum Authors. All rights reserved. 2 // Use of this code is governed by a BSD-style 3 // license that can be found in the LICENSE file 4 5 package cscalar 6 7 import ( 8 "math" 9 "math/cmplx" 10 "testing" 11 ) 12 13 func TestEqualsRelative(t *testing.T) { 14 equalityTests := []struct { 15 a, b float64 16 tol float64 17 equal bool 18 }{ 19 {1000000, 1000001, 0, true}, 20 {1000001, 1000000, 0, true}, 21 {10000, 10001, 0, false}, 22 {10001, 10000, 0, false}, 23 {-1000000, -1000001, 0, true}, 24 {-1000001, -1000000, 0, true}, 25 {-10000, -10001, 0, false}, 26 {-10001, -10000, 0, false}, 27 {1.0000001, 1.0000002, 0, true}, 28 {1.0000002, 1.0000001, 0, true}, 29 {1.0002, 1.0001, 0, false}, 30 {1.0001, 1.0002, 0, false}, 31 {-1.000001, -1.000002, 0, true}, 32 {-1.000002, -1.000001, 0, true}, 33 {-1.0001, -1.0002, 0, false}, 34 {-1.0002, -1.0001, 0, false}, 35 {0.000000001000001, 0.000000001000002, 0, true}, 36 {0.000000001000002, 0.000000001000001, 0, true}, 37 {0.000000000001002, 0.000000000001001, 0, false}, 38 {0.000000000001001, 0.000000000001002, 0, false}, 39 {-0.000000001000001, -0.000000001000002, 0, true}, 40 {-0.000000001000002, -0.000000001000001, 0, true}, 41 {-0.000000000001002, -0.000000000001001, 0, false}, 42 {-0.000000000001001, -0.000000000001002, 0, false}, 43 {0, 0, 0, true}, 44 {0, -0, 0, true}, 45 {-0, -0, 0, true}, 46 {0.00000001, 0, 0, false}, 47 {0, 0.00000001, 0, false}, 48 {-0.00000001, 0, 0, false}, 49 {0, -0.00000001, 0, false}, 50 {0, 1e-310, 0.01, true}, 51 {1e-310, 0, 0.01, true}, 52 {1e-310, 0, 0.000001, false}, 53 {0, 1e-310, 0.000001, false}, 54 {0, -1e-310, 0.1, true}, 55 {-1e-310, 0, 0.1, true}, 56 {-1e-310, 0, 0.00000001, false}, 57 {0, -1e-310, 0.00000001, false}, 58 {math.Inf(1), math.Inf(1), 0, true}, 59 {math.Inf(1), math.MaxFloat64, 0, false}, 60 {math.NaN(), math.NaN(), 0, false}, 61 {math.NaN(), 0, 0, false}, 62 {-0, math.NaN(), 0, false}, 63 {math.NaN(), -0, 0, false}, 64 {0, math.NaN(), 0, false}, 65 {math.NaN(), math.Inf(1), 0, false}, 66 {math.Inf(1), math.NaN(), 0, false}, 67 {math.NaN(), math.MaxFloat64, 0, false}, 68 {math.MaxFloat64, math.NaN(), 0, false}, 69 {math.NaN(), -math.MaxFloat64, 0, false}, 70 {-math.MaxFloat64, math.NaN(), 0, false}, 71 {math.NaN(), math.SmallestNonzeroFloat64, 0, false}, 72 {math.SmallestNonzeroFloat64, math.NaN(), 0, false}, 73 {math.NaN(), -math.SmallestNonzeroFloat64, 0, false}, 74 {-math.SmallestNonzeroFloat64, math.NaN(), 0, false}, 75 {1.000000001, -1.0, 0, false}, 76 {-1.0, 1.000000001, 0, false}, 77 {-1.000000001, 1.0, 0, false}, 78 {1.0, -1.000000001, 0, false}, 79 {10 * math.SmallestNonzeroFloat64, 10 * -math.SmallestNonzeroFloat64, 0, true}, 80 {1e11 * math.SmallestNonzeroFloat64, 1e11 * -math.SmallestNonzeroFloat64, 0, false}, 81 {math.SmallestNonzeroFloat64, -math.SmallestNonzeroFloat64, 0, true}, 82 {-math.SmallestNonzeroFloat64, math.SmallestNonzeroFloat64, 0, true}, 83 {math.SmallestNonzeroFloat64, 0, 0, true}, 84 {0, math.SmallestNonzeroFloat64, 0, true}, 85 {-math.SmallestNonzeroFloat64, 0, 0, true}, 86 {0, -math.SmallestNonzeroFloat64, 0, true}, 87 {0.000000001, -math.SmallestNonzeroFloat64, 0, false}, 88 {0.000000001, math.SmallestNonzeroFloat64, 0, false}, 89 {math.SmallestNonzeroFloat64, 0.000000001, 0, false}, 90 {-math.SmallestNonzeroFloat64, 0.000000001, 0, false}, 91 } 92 for _, ts := range equalityTests { 93 if ts.tol == 0 { 94 ts.tol = 1e-5 95 } 96 97 for _, comp := range []struct{ a, b complex128 }{ 98 {a: complex(ts.a, 0), b: complex(ts.b, 0)}, 99 {a: complex(0, ts.a), b: complex(0, ts.b)}, 100 {a: complex(ts.a, ts.a), b: complex(ts.b, ts.b)}, 101 } { 102 if equal := EqualWithinRel(comp.a, comp.b, ts.tol); equal != ts.equal { 103 t.Errorf("Relative equality of %g and %g with tolerance %g returned: %v. Expected: %v", 104 comp.a, comp.b, ts.tol, equal, ts.equal) 105 } 106 } 107 } 108 } 109 110 func TestRoundEven(t *testing.T) { 111 for _, test := range []struct { 112 x complex128 113 prec int 114 want complex128 115 }{ 116 {x: 0, prec: 1, want: 0}, 117 {x: cmplx.Inf(), prec: 1, want: cmplx.Inf()}, 118 {x: cmplx.NaN(), prec: 1, want: cmplx.NaN()}, 119 {x: func() complex128 { var f complex128; return -f }(), prec: 1, want: 0}, 120 {x: math.MaxFloat64 / 2, prec: 1, want: math.MaxFloat64 / 2}, 121 {x: 1 << 64, prec: 1, want: 1 << 64}, 122 {x: 454.4445, prec: 3, want: 454.444}, 123 {x: 454.44445, prec: 4, want: 454.4444}, 124 {x: 0.42499, prec: 4, want: 0.425}, 125 {x: 0.42599, prec: 4, want: 0.426}, 126 {x: 0.424999999999993, prec: 2, want: 0.42}, 127 {x: 0.425, prec: 2, want: 0.42}, 128 {x: 0.425000000000001, prec: 2, want: 0.43}, 129 {x: 123.4244999999999, prec: 3, want: 123.424}, 130 {x: 123.4245, prec: 3, want: 123.424}, 131 {x: 123.4245000000001, prec: 3, want: 123.425}, 132 133 {x: 454.45, prec: 0, want: 454}, 134 {x: 454.45, prec: 1, want: 454.4}, 135 {x: 454.45, prec: 2, want: 454.45}, 136 {x: 454.45, prec: 3, want: 454.45}, 137 {x: 454.445, prec: 0, want: 454}, 138 {x: 454.445, prec: 1, want: 454.4}, 139 {x: 454.445, prec: 2, want: 454.44}, 140 {x: 454.445, prec: 3, want: 454.445}, 141 {x: 454.445, prec: 4, want: 454.445}, 142 {x: 454.55, prec: 0, want: 455}, 143 {x: 454.55, prec: 1, want: 454.6}, 144 {x: 454.55, prec: 2, want: 454.55}, 145 {x: 454.55, prec: 3, want: 454.55}, 146 {x: 454.455, prec: 0, want: 454}, 147 {x: 454.455, prec: 1, want: 454.5}, 148 {x: 454.455, prec: 2, want: 454.46}, 149 {x: 454.455, prec: 3, want: 454.455}, 150 {x: 454.455, prec: 4, want: 454.455}, 151 152 // Negative precision. 153 {x: 454.45, prec: -1, want: 450}, 154 {x: 454.45, prec: -2, want: 500}, 155 {x: 500, prec: -3, want: 0}, 156 {x: 500, prec: -4, want: 0}, 157 {x: 1500, prec: -3, want: 2000}, 158 {x: 1500, prec: -4, want: 0}, 159 } { 160 for _, sign := range []complex128{1, -1} { 161 got := RoundEven(sign*test.x, test.prec) 162 want := sign * test.want 163 if want == 0 { 164 want = 0 165 } 166 // FIXME(kortschak): Complexify this. 167 if (got != want || math.Signbit(real(got)) != math.Signbit(real(want))) && !(math.IsNaN(real(got)) && math.IsNaN(real(want))) { 168 t.Errorf("unexpected result for RoundEven(%g, %d): got: %g, want: %g", sign*test.x, test.prec, got, want) 169 } 170 } 171 } 172 } 173 174 func TestSame(t *testing.T) { 175 t.Parallel() 176 for _, test := range []struct { 177 a, b complex128 178 want bool 179 }{ 180 {a: 0, b: 0, want: true}, 181 {a: 1, b: 1, want: true}, 182 {a: -1, b: 1, want: false}, 183 {a: 0, b: 1, want: false}, 184 {a: 1, b: 0, want: false}, 185 {a: -1, b: 1, want: false}, 186 {a: cmplx.NaN(), b: cmplx.NaN(), want: true}, 187 {a: 1, b: cmplx.NaN(), want: false}, 188 {a: cmplx.Inf(), b: cmplx.NaN(), want: false}, 189 {a: cmplx.NaN(), b: cmplx.Inf(), want: false}, 190 {a: cmplx.NaN(), b: 1, want: false}, 191 {a: cmplx.Inf(), b: cmplx.Inf(), want: true}, 192 } { 193 got := Same(test.a, test.b) 194 if got != test.want { 195 t.Errorf("unexpected results for a=%f b=%f: got:%t want:%t", test.a, test.b, got, test.want) 196 } 197 } 198 }