Skip to content

Commit c1c1b2c

Browse files
tiehuisIgor Stojkovic
authored and
Igor Stojkovic
committed
std.math.complex: tighten existing test bounds
1 parent 5a55ba1 commit c1c1b2c

19 files changed

+68
-78
lines changed

lib/std/math/complex/abs.zig

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ pub fn abs(z: anytype) @TypeOf(z.re, z.im) {
99
return math.hypot(z.re, z.im);
1010
}
1111

12-
const epsilon = 0.0001;
13-
1412
test abs {
13+
const epsilon = math.floatEps(f32);
1514
const a = Complex(f32).init(5, 3);
1615
const c = abs(a);
17-
try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon));
16+
try testing.expectApproxEqAbs(5.8309517, c, epsilon);
1817
}

lib/std/math/complex/acos.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ pub fn acos(z: anytype) Complex(@TypeOf(z.re, z.im)) {
1111
return Complex(T).init(@as(T, math.pi) / 2 - q.re, -q.im);
1212
}
1313

14-
const epsilon = 0.0001;
15-
1614
test acos {
15+
const epsilon = math.floatEps(f32);
1716
const a = Complex(f32).init(5, 3);
1817
const c = acos(a);
1918

20-
try testing.expect(math.approxEqAbs(f32, c.re, 0.546975, epsilon));
21-
try testing.expect(math.approxEqAbs(f32, c.im, -2.452914, epsilon));
19+
try testing.expectApproxEqAbs(0.5469737, c.re, epsilon);
20+
try testing.expectApproxEqAbs(-2.4529128, c.im, epsilon);
2221
}

lib/std/math/complex/acosh.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ pub fn acosh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
1515
Complex(T).init(-q.im, q.re);
1616
}
1717

18-
const epsilon = 0.0001;
19-
2018
test acosh {
19+
const epsilon = math.floatEps(f32);
2120
const a = Complex(f32).init(5, 3);
2221
const c = acosh(a);
2322

24-
try testing.expect(math.approxEqAbs(f32, c.re, 2.452914, epsilon));
25-
try testing.expect(math.approxEqAbs(f32, c.im, 0.546975, epsilon));
23+
try testing.expectApproxEqAbs(2.4529128, c.re, epsilon);
24+
try testing.expectApproxEqAbs(0.5469737, c.im, epsilon);
2625
}

lib/std/math/complex/arg.zig

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ pub fn arg(z: anytype) @TypeOf(z.re, z.im) {
99
return math.atan2(z.im, z.re);
1010
}
1111

12-
const epsilon = 0.0001;
13-
1412
test arg {
13+
const epsilon = math.floatEps(f32);
1514
const a = Complex(f32).init(5, 3);
1615
const c = arg(a);
17-
try testing.expect(math.approxEqAbs(f32, c, 0.540420, epsilon));
16+
try testing.expectApproxEqAbs(0.5404195, c, epsilon);
1817
}

lib/std/math/complex/asin.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ pub fn asin(z: anytype) Complex(@TypeOf(z.re, z.im)) {
1717
return Complex(T).init(r.im, -r.re);
1818
}
1919

20-
const epsilon = 0.0001;
21-
2220
test asin {
21+
const epsilon = math.floatEps(f32);
2322
const a = Complex(f32).init(5, 3);
2423
const c = asin(a);
2524

26-
try testing.expect(math.approxEqAbs(f32, c.re, 1.023822, epsilon));
27-
try testing.expect(math.approxEqAbs(f32, c.im, 2.452914, epsilon));
25+
try testing.expectApproxEqAbs(1.0238227, c.re, epsilon);
26+
try testing.expectApproxEqAbs(2.4529128, c.im, epsilon);
2827
}

lib/std/math/complex/asinh.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ pub fn asinh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
1212
return Complex(T).init(r.im, -r.re);
1313
}
1414

15-
const epsilon = 0.0001;
16-
1715
test asinh {
16+
const epsilon = math.floatEps(f32);
1817
const a = Complex(f32).init(5, 3);
1918
const c = asinh(a);
2019

21-
try testing.expect(math.approxEqAbs(f32, c.re, 2.459831, epsilon));
22-
try testing.expect(math.approxEqAbs(f32, c.im, 0.533999, epsilon));
20+
try testing.expectApproxEqAbs(2.4598298, c.re, epsilon);
21+
try testing.expectApproxEqAbs(0.5339993, c.im, epsilon);
2322
}

lib/std/math/complex/atan.zig

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,20 @@ fn atan64(z: Complex(f64)) Complex(f64) {
8888
return Complex(f64).init(w, 0.25 * @log(a));
8989
}
9090

91-
const epsilon = 0.0001;
92-
9391
test atan32 {
92+
const epsilon = math.floatEps(f32);
9493
const a = Complex(f32).init(5, 3);
9594
const c = atan(a);
9695

97-
try testing.expect(math.approxEqAbs(f32, c.re, 1.423679, epsilon));
98-
try testing.expect(math.approxEqAbs(f32, c.im, 0.086569, epsilon));
96+
try testing.expectApproxEqAbs(1.423679, c.re, epsilon);
97+
try testing.expectApproxEqAbs(0.086569, c.im, epsilon);
9998
}
10099

101100
test atan64 {
101+
const epsilon = math.floatEps(f64);
102102
const a = Complex(f64).init(5, 3);
103103
const c = atan(a);
104104

105-
try testing.expect(math.approxEqAbs(f64, c.re, 1.423679, epsilon));
106-
try testing.expect(math.approxEqAbs(f64, c.im, 0.086569, epsilon));
105+
try testing.expectApproxEqAbs(1.4236790442393028, c.re, epsilon);
106+
try testing.expectApproxEqAbs(0.08656905917945844, c.im, epsilon);
107107
}

lib/std/math/complex/atanh.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ pub fn atanh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
1212
return Complex(T).init(r.im, -r.re);
1313
}
1414

15-
const epsilon = 0.0001;
16-
1715
test atanh {
16+
const epsilon = math.floatEps(f32);
1817
const a = Complex(f32).init(5, 3);
1918
const c = atanh(a);
2019

21-
try testing.expect(math.approxEqAbs(f32, c.re, 0.146947, epsilon));
22-
try testing.expect(math.approxEqAbs(f32, c.im, 1.480870, epsilon));
20+
try testing.expectApproxEqAbs(0.14694665, c.re, epsilon);
21+
try testing.expectApproxEqAbs(1.4808695, c.im, epsilon);
2322
}

lib/std/math/complex/conj.zig

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ test conj {
1414
const a = Complex(f32).init(5, 3);
1515
const c = a.conjugate();
1616

17-
try testing.expect(c.re == 5 and c.im == -3);
17+
try testing.expectEqual(5, c.re);
18+
try testing.expectEqual(-3, c.im);
1819
}

lib/std/math/complex/cos.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ pub fn cos(z: anytype) Complex(@TypeOf(z.re, z.im)) {
1111
return cmath.cosh(p);
1212
}
1313

14-
const epsilon = 0.0001;
15-
1614
test cos {
15+
const epsilon = math.floatEps(f32);
1716
const a = Complex(f32).init(5, 3);
1817
const c = cos(a);
1918

20-
try testing.expect(math.approxEqAbs(f32, c.re, 2.855815, epsilon));
21-
try testing.expect(math.approxEqAbs(f32, c.im, 9.606383, epsilon));
19+
try testing.expectApproxEqAbs(2.8558152, c.re, epsilon);
20+
try testing.expectApproxEqAbs(9.606383, c.im, epsilon);
2221
}

lib/std/math/complex/cosh.zig

+6-6
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,20 @@ fn cosh64(z: Complex(f64)) Complex(f64) {
153153
return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
154154
}
155155

156-
const epsilon = 0.0001;
157-
158156
test cosh32 {
157+
const epsilon = math.floatEps(f32);
159158
const a = Complex(f32).init(5, 3);
160159
const c = cosh(a);
161160

162-
try testing.expect(math.approxEqAbs(f32, c.re, -73.467300, epsilon));
163-
try testing.expect(math.approxEqAbs(f32, c.im, 10.471557, epsilon));
161+
try testing.expectApproxEqAbs(-73.467300, c.re, epsilon);
162+
try testing.expectApproxEqAbs(10.471557, c.im, epsilon);
164163
}
165164

166165
test cosh64 {
166+
const epsilon = math.floatEps(f64);
167167
const a = Complex(f64).init(5, 3);
168168
const c = cosh(a);
169169

170-
try testing.expect(math.approxEqAbs(f64, c.re, -73.467300, epsilon));
171-
try testing.expect(math.approxEqAbs(f64, c.im, 10.471557, epsilon));
170+
try testing.expectApproxEqAbs(-73.46729221264526, c.re, epsilon);
171+
try testing.expectApproxEqAbs(10.471557674805572, c.im, epsilon);
172172
}

lib/std/math/complex/log.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ pub fn log(z: anytype) Complex(@TypeOf(z.re, z.im)) {
1313
return Complex(T).init(@log(r), phi);
1414
}
1515

16-
const epsilon = 0.0001;
17-
1816
test log {
17+
const epsilon = math.floatEps(f32);
1918
const a = Complex(f32).init(5, 3);
2019
const c = log(a);
2120

22-
try testing.expect(math.approxEqAbs(f32, c.re, 1.763180, epsilon));
23-
try testing.expect(math.approxEqAbs(f32, c.im, 0.540419, epsilon));
21+
try testing.expectApproxEqAbs(1.7631803, c.re, epsilon);
22+
try testing.expectApproxEqAbs(0.5404195, c.im, epsilon);
2423
}

lib/std/math/complex/pow.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ pub fn pow(z: anytype, s: anytype) Complex(@TypeOf(z.re, z.im, s.re, s.im)) {
99
return cmath.exp(cmath.log(z).mul(s));
1010
}
1111

12-
const epsilon = 0.0001;
13-
1412
test pow {
13+
const epsilon = math.floatEps(f32);
1514
const a = Complex(f32).init(5, 3);
1615
const b = Complex(f32).init(2.3, -1.3);
1716
const c = pow(a, b);
1817

19-
try testing.expect(math.approxEqAbs(f32, c.re, 58.049110, epsilon));
20-
try testing.expect(math.approxEqAbs(f32, c.im, -101.003433, epsilon));
18+
try testing.expectApproxEqAbs(58.049110, c.re, epsilon);
19+
try testing.expectApproxEqAbs(-101.003433, c.im, epsilon);
2120
}

lib/std/math/complex/proj.zig

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ test proj {
1919
const a = Complex(f32).init(5, 3);
2020
const c = proj(a);
2121

22-
try testing.expect(c.re == 5 and c.im == 3);
22+
try testing.expectEqual(5, c.re);
23+
try testing.expectEqual(3, c.im);
2324
}

lib/std/math/complex/sin.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ pub fn sin(z: anytype) Complex(@TypeOf(z.re, z.im)) {
1212
return Complex(T).init(q.im, -q.re);
1313
}
1414

15-
const epsilon = 0.0001;
16-
1715
test sin {
16+
const epsilon = math.floatEps(f32);
1817
const a = Complex(f32).init(5, 3);
1918
const c = sin(a);
2019

21-
try testing.expect(math.approxEqAbs(f32, c.re, -9.654126, epsilon));
22-
try testing.expect(math.approxEqAbs(f32, c.im, 2.841692, epsilon));
20+
try testing.expectApproxEqAbs(-9.654126, c.re, epsilon);
21+
try testing.expectApproxEqAbs(2.8416924, c.im, epsilon);
2322
}

lib/std/math/complex/sinh.zig

+6-6
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,20 @@ fn sinh64(z: Complex(f64)) Complex(f64) {
152152
return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y));
153153
}
154154

155-
const epsilon = 0.0001;
156-
157155
test sinh32 {
156+
const epsilon = math.floatEps(f32);
158157
const a = Complex(f32).init(5, 3);
159158
const c = sinh(a);
160159

161-
try testing.expect(math.approxEqAbs(f32, c.re, -73.460617, epsilon));
162-
try testing.expect(math.approxEqAbs(f32, c.im, 10.472508, epsilon));
160+
try testing.expectApproxEqAbs(-73.460617, c.re, epsilon);
161+
try testing.expectApproxEqAbs(10.472508, c.im, epsilon);
163162
}
164163

165164
test sinh64 {
165+
const epsilon = math.floatEps(f64);
166166
const a = Complex(f64).init(5, 3);
167167
const c = sinh(a);
168168

169-
try testing.expect(math.approxEqAbs(f64, c.re, -73.460617, epsilon));
170-
try testing.expect(math.approxEqAbs(f64, c.im, 10.472508, epsilon));
169+
try testing.expectApproxEqAbs(-73.46062169567367, c.re, epsilon);
170+
try testing.expectApproxEqAbs(10.472508533940392, c.im, epsilon);
171171
}

lib/std/math/complex/sqrt.zig

+6-6
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,20 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
127127
return result;
128128
}
129129

130-
const epsilon = 0.0001;
131-
132130
test sqrt32 {
131+
const epsilon = math.floatEps(f32);
133132
const a = Complex(f32).init(5, 3);
134133
const c = sqrt(a);
135134

136-
try testing.expect(math.approxEqAbs(f32, c.re, 2.327117, epsilon));
137-
try testing.expect(math.approxEqAbs(f32, c.im, 0.644574, epsilon));
135+
try testing.expectApproxEqAbs(2.3271174, c.re, epsilon);
136+
try testing.expectApproxEqAbs(0.6445742, c.im, epsilon);
138137
}
139138

140139
test sqrt64 {
140+
const epsilon = math.floatEps(f64);
141141
const a = Complex(f64).init(5, 3);
142142
const c = sqrt(a);
143143

144-
try testing.expect(math.approxEqAbs(f64, c.re, 2.3271175190399496, epsilon));
145-
try testing.expect(math.approxEqAbs(f64, c.im, 0.6445742373246469, epsilon));
144+
try testing.expectApproxEqAbs(2.3271175190399496, c.re, epsilon);
145+
try testing.expectApproxEqAbs(0.6445742373246469, c.im, epsilon);
146146
}

lib/std/math/complex/tan.zig

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ pub fn tan(z: anytype) Complex(@TypeOf(z.re, z.im)) {
1212
return Complex(T).init(r.im, -r.re);
1313
}
1414

15-
const epsilon = 0.0001;
16-
1715
test tan {
16+
const epsilon = math.floatEps(f32);
1817
const a = Complex(f32).init(5, 3);
1918
const c = tan(a);
2019

21-
try testing.expect(math.approxEqAbs(f32, c.re, -0.002708233, epsilon));
22-
try testing.expect(math.approxEqAbs(f32, c.im, 1.004165, epsilon));
20+
try testing.expectApproxEqAbs(-0.002708233, c.re, epsilon);
21+
try testing.expectApproxEqAbs(1.0041647, c.im, epsilon);
2322
}

lib/std/math/complex/tanh.zig

+6-6
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,20 @@ fn tanh64(z: Complex(f64)) Complex(f64) {
101101
return Complex(f64).init((beta * rho * s) / den, t / den);
102102
}
103103

104-
const epsilon = 0.0001;
105-
106104
test tanh32 {
105+
const epsilon = math.floatEps(f32);
107106
const a = Complex(f32).init(5, 3);
108107
const c = tanh(a);
109108

110-
try testing.expect(math.approxEqAbs(f32, c.re, 0.999913, epsilon));
111-
try testing.expect(math.approxEqAbs(f32, c.im, -0.000025, epsilon));
109+
try testing.expectApproxEqAbs(0.99991274, c.re, epsilon);
110+
try testing.expectApproxEqAbs(-0.00002536878, c.im, epsilon);
112111
}
113112

114113
test tanh64 {
114+
const epsilon = math.floatEps(f64);
115115
const a = Complex(f64).init(5, 3);
116116
const c = tanh(a);
117117

118-
try testing.expect(math.approxEqAbs(f64, c.re, 0.999913, epsilon));
119-
try testing.expect(math.approxEqAbs(f64, c.im, -0.000025, epsilon));
118+
try testing.expectApproxEqAbs(0.9999128201513536, c.re, epsilon);
119+
try testing.expectApproxEqAbs(-0.00002536867620767604, c.im, epsilon);
120120
}

0 commit comments

Comments
 (0)