| author | |
| committer | |
| log | 059856acfc9f87d723a90af6a4214e128b8cae2e |
| tree | 851538dd757a467513f7061db4d2ec9b94a2c628 |
| parent | 7c5ee3efde6d948205c6f6eaa7ab52bda3715fea |
| parent | 843885512dd69e083ec9163e6f57822487b46639 |
| signature |
std.math.complex fixes19 files changed, 102 insertions(+), 120 deletions(-)
lib/std/math/complex/abs.zig+2-3| ... | @@ -9,10 +9,9 @@ pub fn abs(z: anytype) @TypeOf(z.re, z.im) { | ... | @@ -9,10 +9,9 @@ pub fn abs(z: anytype) @TypeOf(z.re, z.im) { |
| 9 | return math.hypot(z.re, z.im); | 9 | return math.hypot(z.re, z.im); |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | const epsilon = 0.0001; | ||
| 13 | |||
| 14 | test abs { | 12 | test abs { |
| 13 | const epsilon = math.floatEps(f32); | ||
| 15 | const a = Complex(f32).init(5, 3); | 14 | const a = Complex(f32).init(5, 3); |
| 16 | const c = abs(a); | 15 | const c = abs(a); |
| 17 | try testing.expect(math.approxEqAbs(f32, c, 5.83095, epsilon)); | 16 | try testing.expectApproxEqAbs(5.8309517, c, epsilon); |
| 18 | } | 17 | } |
lib/std/math/complex/acos.zig+3-4| ... | @@ -11,12 +11,11 @@ pub fn acos(z: anytype) Complex(@TypeOf(z.re, z.im)) { | ... | @@ -11,12 +11,11 @@ pub fn acos(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 11 | return Complex(T).init(@as(T, math.pi) / 2 - q.re, -q.im); | 11 | return Complex(T).init(@as(T, math.pi) / 2 - q.re, -q.im); |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | const epsilon = 0.0001; | ||
| 15 | |||
| 16 | test acos { | 14 | test acos { |
| 15 | const epsilon = math.floatEps(f32); | ||
| 17 | const a = Complex(f32).init(5, 3); | 16 | const a = Complex(f32).init(5, 3); |
| 18 | const c = acos(a); | 17 | const c = acos(a); |
| 19 | 18 | ||
| 20 | try testing.expect(math.approxEqAbs(f32, c.re, 0.546975, epsilon)); | 19 | try testing.expectApproxEqAbs(0.5469737, c.re, epsilon); |
| 21 | try testing.expect(math.approxEqAbs(f32, c.im, -2.452914, epsilon)); | 20 | try testing.expectApproxEqAbs(-2.4529128, c.im, epsilon); |
| 22 | } | 21 | } |
lib/std/math/complex/acosh.zig+8-5| ... | @@ -8,15 +8,18 @@ const Complex = cmath.Complex; | ... | @@ -8,15 +8,18 @@ const Complex = cmath.Complex; |
| 8 | pub fn acosh(z: anytype) Complex(@TypeOf(z.re, z.im)) { | 8 | pub fn acosh(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 9 | const T = @TypeOf(z.re, z.im); | 9 | const T = @TypeOf(z.re, z.im); |
| 10 | const q = cmath.acos(z); | 10 | const q = cmath.acos(z); |
| 11 | return Complex(T).init(-q.im, q.re); | ||
| 12 | } | ||
| 13 | 11 | ||
| 14 | const epsilon = 0.0001; | 12 | return if (math.signbit(z.im)) |
| 13 | Complex(T).init(q.im, -q.re) | ||
| 14 | else | ||
| 15 | Complex(T).init(-q.im, q.re); | ||
| 16 | } | ||
| 15 | 17 | ||
| 16 | test acosh { | 18 | test acosh { |
| 19 | const epsilon = math.floatEps(f32); | ||
| 17 | const a = Complex(f32).init(5, 3); | 20 | const a = Complex(f32).init(5, 3); |
| 18 | const c = acosh(a); | 21 | const c = acosh(a); |
| 19 | 22 | ||
| 20 | try testing.expect(math.approxEqAbs(f32, c.re, 2.452914, epsilon)); | 23 | try testing.expectApproxEqAbs(2.4529128, c.re, epsilon); |
| 21 | try testing.expect(math.approxEqAbs(f32, c.im, 0.546975, epsilon)); | 24 | try testing.expectApproxEqAbs(0.5469737, c.im, epsilon); |
| 22 | } | 25 | } |
lib/std/math/complex/arg.zig+2-3| ... | @@ -9,10 +9,9 @@ pub fn arg(z: anytype) @TypeOf(z.re, z.im) { | ... | @@ -9,10 +9,9 @@ pub fn arg(z: anytype) @TypeOf(z.re, z.im) { |
| 9 | return math.atan2(z.im, z.re); | 9 | return math.atan2(z.im, z.re); |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | const epsilon = 0.0001; | ||
| 13 | |||
| 14 | test arg { | 12 | test arg { |
| 13 | const epsilon = math.floatEps(f32); | ||
| 15 | const a = Complex(f32).init(5, 3); | 14 | const a = Complex(f32).init(5, 3); |
| 16 | const c = arg(a); | 15 | const c = arg(a); |
| 17 | try testing.expect(math.approxEqAbs(f32, c, 0.540420, epsilon)); | 16 | try testing.expectApproxEqAbs(0.5404195, c, epsilon); |
| 18 | } | 17 | } |
lib/std/math/complex/asin.zig+3-4| ... | @@ -17,12 +17,11 @@ pub fn asin(z: anytype) Complex(@TypeOf(z.re, z.im)) { | ... | @@ -17,12 +17,11 @@ pub fn asin(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 17 | return Complex(T).init(r.im, -r.re); | 17 | return Complex(T).init(r.im, -r.re); |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | const epsilon = 0.0001; | ||
| 21 | |||
| 22 | test asin { | 20 | test asin { |
| 21 | const epsilon = math.floatEps(f32); | ||
| 23 | const a = Complex(f32).init(5, 3); | 22 | const a = Complex(f32).init(5, 3); |
| 24 | const c = asin(a); | 23 | const c = asin(a); |
| 25 | 24 | ||
| 26 | try testing.expect(math.approxEqAbs(f32, c.re, 1.023822, epsilon)); | 25 | try testing.expectApproxEqAbs(1.0238227, c.re, epsilon); |
| 27 | try testing.expect(math.approxEqAbs(f32, c.im, 2.452914, epsilon)); | 26 | try testing.expectApproxEqAbs(2.4529128, c.im, epsilon); |
| 28 | } | 27 | } |
lib/std/math/complex/asinh.zig+3-4| ... | @@ -12,12 +12,11 @@ pub fn asinh(z: anytype) Complex(@TypeOf(z.re, z.im)) { | ... | @@ -12,12 +12,11 @@ pub fn asinh(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 12 | return Complex(T).init(r.im, -r.re); | 12 | return Complex(T).init(r.im, -r.re); |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | const epsilon = 0.0001; | ||
| 16 | |||
| 17 | test asinh { | 15 | test asinh { |
| 16 | const epsilon = math.floatEps(f32); | ||
| 18 | const a = Complex(f32).init(5, 3); | 17 | const a = Complex(f32).init(5, 3); |
| 19 | const c = asinh(a); | 18 | const c = asinh(a); |
| 20 | 19 | ||
| 21 | try testing.expect(math.approxEqAbs(f32, c.re, 2.459831, epsilon)); | 20 | try testing.expectApproxEqAbs(2.4598298, c.re, epsilon); |
| 22 | try testing.expect(math.approxEqAbs(f32, c.im, 0.533999, epsilon)); | 21 | try testing.expectApproxEqAbs(0.5339993, c.im, epsilon); |
| 23 | } | 22 | } |
lib/std/math/complex/atan.zig+10-40| ... | @@ -32,37 +32,22 @@ fn redupif32(x: f32) f32 { | ... | @@ -32,37 +32,22 @@ fn redupif32(x: f32) f32 { |
| 32 | t -= 0.5; | 32 | t -= 0.5; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | const u = @as(f32, @floatFromInt(@as(i32, @intFromFloat(t)))); | 35 | const u: f32 = @trunc(t); |
| 36 | return ((x - u * DP1) - u * DP2) - t * DP3; | 36 | return ((x - u * DP1) - u * DP2) - u * DP3; |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | fn atan32(z: Complex(f32)) Complex(f32) { | 39 | fn atan32(z: Complex(f32)) Complex(f32) { |
| 40 | const maxnum = 1.0e38; | ||
| 41 | |||
| 42 | const x = z.re; | 40 | const x = z.re; |
| 43 | const y = z.im; | 41 | const y = z.im; |
| 44 | 42 | ||
| 45 | if ((x == 0.0) and (y > 1.0)) { | ||
| 46 | // overflow | ||
| 47 | return Complex(f32).init(maxnum, maxnum); | ||
| 48 | } | ||
| 49 | |||
| 50 | const x2 = x * x; | 43 | const x2 = x * x; |
| 51 | var a = 1.0 - x2 - (y * y); | 44 | var a = 1.0 - x2 - (y * y); |
| 52 | if (a == 0.0) { | ||
| 53 | // overflow | ||
| 54 | return Complex(f32).init(maxnum, maxnum); | ||
| 55 | } | ||
| 56 | 45 | ||
| 57 | var t = 0.5 * math.atan2(2.0 * x, a); | 46 | var t = 0.5 * math.atan2(2.0 * x, a); |
| 58 | const w = redupif32(t); | 47 | const w = redupif32(t); |
| 59 | 48 | ||
| 60 | t = y - 1.0; | 49 | t = y - 1.0; |
| 61 | a = x2 + t * t; | 50 | a = x2 + t * t; |
| 62 | if (a == 0.0) { | ||
| 63 | // overflow | ||
| 64 | return Complex(f32).init(maxnum, maxnum); | ||
| 65 | } | ||
| 66 | 51 | ||
| 67 | t = y + 1.0; | 52 | t = y + 1.0; |
| 68 | a = (x2 + (t * t)) / a; | 53 | a = (x2 + (t * t)) / a; |
| ... | @@ -81,57 +66,42 @@ fn redupif64(x: f64) f64 { | ... | @@ -81,57 +66,42 @@ fn redupif64(x: f64) f64 { |
| 81 | t -= 0.5; | 66 | t -= 0.5; |
| 82 | } | 67 | } |
| 83 | 68 | ||
| 84 | const u = @as(f64, @floatFromInt(@as(i64, @intFromFloat(t)))); | 69 | const u: f64 = @trunc(t); |
| 85 | return ((x - u * DP1) - u * DP2) - t * DP3; | 70 | return ((x - u * DP1) - u * DP2) - u * DP3; |
| 86 | } | 71 | } |
| 87 | 72 | ||
| 88 | fn atan64(z: Complex(f64)) Complex(f64) { | 73 | fn atan64(z: Complex(f64)) Complex(f64) { |
| 89 | const maxnum = 1.0e308; | ||
| 90 | |||
| 91 | const x = z.re; | 74 | const x = z.re; |
| 92 | const y = z.im; | 75 | const y = z.im; |
| 93 | 76 | ||
| 94 | if ((x == 0.0) and (y > 1.0)) { | ||
| 95 | // overflow | ||
| 96 | return Complex(f64).init(maxnum, maxnum); | ||
| 97 | } | ||
| 98 | |||
| 99 | const x2 = x * x; | 77 | const x2 = x * x; |
| 100 | var a = 1.0 - x2 - (y * y); | 78 | var a = 1.0 - x2 - (y * y); |
| 101 | if (a == 0.0) { | ||
| 102 | // overflow | ||
| 103 | return Complex(f64).init(maxnum, maxnum); | ||
| 104 | } | ||
| 105 | 79 | ||
| 106 | var t = 0.5 * math.atan2(2.0 * x, a); | 80 | var t = 0.5 * math.atan2(2.0 * x, a); |
| 107 | const w = redupif64(t); | 81 | const w = redupif64(t); |
| 108 | 82 | ||
| 109 | t = y - 1.0; | 83 | t = y - 1.0; |
| 110 | a = x2 + t * t; | 84 | a = x2 + t * t; |
| 111 | if (a == 0.0) { | ||
| 112 | // overflow | ||
| 113 | return Complex(f64).init(maxnum, maxnum); | ||
| 114 | } | ||
| 115 | 85 | ||
| 116 | t = y + 1.0; | 86 | t = y + 1.0; |
| 117 | a = (x2 + (t * t)) / a; | 87 | a = (x2 + (t * t)) / a; |
| 118 | return Complex(f64).init(w, 0.25 * @log(a)); | 88 | return Complex(f64).init(w, 0.25 * @log(a)); |
| 119 | } | 89 | } |
| 120 | 90 | ||
| 121 | const epsilon = 0.0001; | ||
| 122 | |||
| 123 | test atan32 { | 91 | test atan32 { |
| 92 | const epsilon = math.floatEps(f32); | ||
| 124 | const a = Complex(f32).init(5, 3); | 93 | const a = Complex(f32).init(5, 3); |
| 125 | const c = atan(a); | 94 | const c = atan(a); |
| 126 | 95 | ||
| 127 | try testing.expect(math.approxEqAbs(f32, c.re, 1.423679, epsilon)); | 96 | try testing.expectApproxEqAbs(1.423679, c.re, epsilon); |
| 128 | try testing.expect(math.approxEqAbs(f32, c.im, 0.086569, epsilon)); | 97 | try testing.expectApproxEqAbs(0.086569, c.im, epsilon); |
| 129 | } | 98 | } |
| 130 | 99 | ||
| 131 | test atan64 { | 100 | test atan64 { |
| 101 | const epsilon = math.floatEps(f64); | ||
| 132 | const a = Complex(f64).init(5, 3); | 102 | const a = Complex(f64).init(5, 3); |
| 133 | const c = atan(a); | 103 | const c = atan(a); |
| 134 | 104 | ||
| 135 | try testing.expect(math.approxEqAbs(f64, c.re, 1.423679, epsilon)); | 105 | try testing.expectApproxEqAbs(1.4236790442393028, c.re, epsilon); |
| 136 | try testing.expect(math.approxEqAbs(f64, c.im, 0.086569, epsilon)); | 106 | try testing.expectApproxEqAbs(0.08656905917945844, c.im, epsilon); |
| 137 | } | 107 | } |
lib/std/math/complex/atanh.zig+3-4| ... | @@ -12,12 +12,11 @@ pub fn atanh(z: anytype) Complex(@TypeOf(z.re, z.im)) { | ... | @@ -12,12 +12,11 @@ pub fn atanh(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 12 | return Complex(T).init(r.im, -r.re); | 12 | return Complex(T).init(r.im, -r.re); |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | const epsilon = 0.0001; | ||
| 16 | |||
| 17 | test atanh { | 15 | test atanh { |
| 16 | const epsilon = math.floatEps(f32); | ||
| 18 | const a = Complex(f32).init(5, 3); | 17 | const a = Complex(f32).init(5, 3); |
| 19 | const c = atanh(a); | 18 | const c = atanh(a); |
| 20 | 19 | ||
| 21 | try testing.expect(math.approxEqAbs(f32, c.re, 0.146947, epsilon)); | 20 | try testing.expectApproxEqAbs(0.14694665, c.re, epsilon); |
| 22 | try testing.expect(math.approxEqAbs(f32, c.im, 1.480870, epsilon)); | 21 | try testing.expectApproxEqAbs(1.4808695, c.im, epsilon); |
| 23 | } | 22 | } |
lib/std/math/complex/conj.zig+2-1| ... | @@ -14,5 +14,6 @@ test conj { | ... | @@ -14,5 +14,6 @@ test conj { |
| 14 | const a = Complex(f32).init(5, 3); | 14 | const a = Complex(f32).init(5, 3); |
| 15 | const c = a.conjugate(); | 15 | const c = a.conjugate(); |
| 16 | 16 | ||
| 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); | ||
| 18 | } | 19 | } |
lib/std/math/complex/cos.zig+3-4| ... | @@ -11,12 +11,11 @@ pub fn cos(z: anytype) Complex(@TypeOf(z.re, z.im)) { | ... | @@ -11,12 +11,11 @@ pub fn cos(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 11 | return cmath.cosh(p); | 11 | return cmath.cosh(p); |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | const epsilon = 0.0001; | ||
| 15 | |||
| 16 | test cos { | 14 | test cos { |
| 15 | const epsilon = math.floatEps(f32); | ||
| 17 | const a = Complex(f32).init(5, 3); | 16 | const a = Complex(f32).init(5, 3); |
| 18 | const c = cos(a); | 17 | const c = cos(a); |
| 19 | 18 | ||
| 20 | try testing.expect(math.approxEqAbs(f32, c.re, 2.855815, epsilon)); | 19 | try testing.expectApproxEqAbs(2.8558152, c.re, epsilon); |
| 21 | try testing.expect(math.approxEqAbs(f32, c.im, 9.606383, epsilon)); | 20 | try testing.expectApproxEqAbs(9.606383, c.im, epsilon); |
| 22 | } | 21 | } |
lib/std/math/complex/cosh.zig+19-10| ... | @@ -34,7 +34,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) { | ... | @@ -34,7 +34,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) { |
| 34 | 34 | ||
| 35 | if (ix < 0x7f800000 and iy < 0x7f800000) { | 35 | if (ix < 0x7f800000 and iy < 0x7f800000) { |
| 36 | if (iy == 0) { | 36 | if (iy == 0) { |
| 37 | return Complex(f32).init(math.cosh(x), y); | 37 | return Complex(f32).init(math.cosh(x), x * y); |
| 38 | } | 38 | } |
| 39 | // small x: normal case | 39 | // small x: normal case |
| 40 | if (ix < 0x41100000) { | 40 | if (ix < 0x41100000) { |
| ... | @@ -45,7 +45,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) { | ... | @@ -45,7 +45,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) { |
| 45 | if (ix < 0x42b17218) { | 45 | if (ix < 0x42b17218) { |
| 46 | // x < 88.7: exp(|x|) won't overflow | 46 | // x < 88.7: exp(|x|) won't overflow |
| 47 | const h = @exp(@abs(x)) * 0.5; | 47 | const h = @exp(@abs(x)) * 0.5; |
| 48 | return Complex(f32).init(math.copysign(h, x) * @cos(y), h * @sin(y)); | 48 | return Complex(f32).init(h * @cos(y), math.copysign(h, x) * @sin(y)); |
| 49 | } | 49 | } |
| 50 | // x < 192.7: scale to avoid overflow | 50 | // x < 192.7: scale to avoid overflow |
| 51 | else if (ix < 0x4340b1e7) { | 51 | else if (ix < 0x4340b1e7) { |
| ... | @@ -68,7 +68,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) { | ... | @@ -68,7 +68,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) { |
| 68 | if (hx & 0x7fffff == 0) { | 68 | if (hx & 0x7fffff == 0) { |
| 69 | return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), x) * y); | 69 | return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), x) * y); |
| 70 | } | 70 | } |
| 71 | return Complex(f32).init(x, math.copysign(@as(f32, 0.0), (x + x) * y)); | 71 | return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), (x + x) * y)); |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | if (ix < 0x7f800000 and iy >= 0x7f800000) { | 74 | if (ix < 0x7f800000 and iy >= 0x7f800000) { |
| ... | @@ -123,7 +123,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) { | ... | @@ -123,7 +123,7 @@ fn cosh64(z: Complex(f64)) Complex(f64) { |
| 123 | } | 123 | } |
| 124 | // x >= 1455: result always overflows | 124 | // x >= 1455: result always overflows |
| 125 | else { | 125 | else { |
| 126 | const h = 0x1p1023; | 126 | const h = 0x1p1023 * x; |
| 127 | return Complex(f64).init(h * h * @cos(y), h * @sin(y)); | 127 | return Complex(f64).init(h * h * @cos(y), h * @sin(y)); |
| 128 | } | 128 | } |
| 129 | } | 129 | } |
| ... | @@ -153,20 +153,29 @@ fn cosh64(z: Complex(f64)) Complex(f64) { | ... | @@ -153,20 +153,29 @@ fn cosh64(z: Complex(f64)) Complex(f64) { |
| 153 | return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y)); | 153 | return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y)); |
| 154 | } | 154 | } |
| 155 | 155 | ||
| 156 | const epsilon = 0.0001; | ||
| 157 | |||
| 158 | test cosh32 { | 156 | test cosh32 { |
| 157 | const epsilon = math.floatEps(f32); | ||
| 159 | const a = Complex(f32).init(5, 3); | 158 | const a = Complex(f32).init(5, 3); |
| 160 | const c = cosh(a); | 159 | const c = cosh(a); |
| 161 | 160 | ||
| 162 | try testing.expect(math.approxEqAbs(f32, c.re, -73.467300, epsilon)); | 161 | try testing.expectApproxEqAbs(-73.467300, c.re, epsilon); |
| 163 | try testing.expect(math.approxEqAbs(f32, c.im, 10.471557, epsilon)); | 162 | try testing.expectApproxEqAbs(10.471557, c.im, epsilon); |
| 164 | } | 163 | } |
| 165 | 164 | ||
| 166 | test cosh64 { | 165 | test cosh64 { |
| 166 | const epsilon = math.floatEps(f64); | ||
| 167 | const a = Complex(f64).init(5, 3); | 167 | const a = Complex(f64).init(5, 3); |
| 168 | const c = cosh(a); | 168 | const c = cosh(a); |
| 169 | 169 | ||
| 170 | try testing.expect(math.approxEqAbs(f64, c.re, -73.467300, epsilon)); | 170 | try testing.expectApproxEqAbs(-73.46729221264526, c.re, epsilon); |
| 171 | try testing.expect(math.approxEqAbs(f64, c.im, 10.471557, epsilon)); | 171 | try testing.expectApproxEqAbs(10.471557674805572, c.im, epsilon); |
| 172 | } | ||
| 173 | |||
| 174 | test "cosh64 musl" { | ||
| 175 | const epsilon = math.floatEps(f64); | ||
| 176 | const a = Complex(f64).init(7.44648873421389e17, 1.6008058402057622e19); | ||
| 177 | const c = cosh(a); | ||
| 178 | |||
| 179 | try testing.expectApproxEqAbs(std.math.inf(f64), c.re, epsilon); | ||
| 180 | try testing.expectApproxEqAbs(std.math.inf(f64), c.im, epsilon); | ||
| 172 | } | 181 | } |
lib/std/math/complex/log.zig+3-4| ... | @@ -13,12 +13,11 @@ pub fn log(z: anytype) Complex(@TypeOf(z.re, z.im)) { | ... | @@ -13,12 +13,11 @@ pub fn log(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 13 | return Complex(T).init(@log(r), phi); | 13 | return Complex(T).init(@log(r), phi); |
| 14 | } | 14 | } |
| 15 | 15 | ||
| 16 | const epsilon = 0.0001; | ||
| 17 | |||
| 18 | test log { | 16 | test log { |
| 17 | const epsilon = math.floatEps(f32); | ||
| 19 | const a = Complex(f32).init(5, 3); | 18 | const a = Complex(f32).init(5, 3); |
| 20 | const c = log(a); | 19 | const c = log(a); |
| 21 | 20 | ||
| 22 | try testing.expect(math.approxEqAbs(f32, c.re, 1.763180, epsilon)); | 21 | try testing.expectApproxEqAbs(1.7631803, c.re, epsilon); |
| 23 | try testing.expect(math.approxEqAbs(f32, c.im, 0.540419, epsilon)); | 22 | try testing.expectApproxEqAbs(0.5404195, c.im, epsilon); |
| 24 | } | 23 | } |
lib/std/math/complex/pow.zig+3-4| ... | @@ -9,13 +9,12 @@ pub fn pow(z: anytype, s: anytype) Complex(@TypeOf(z.re, z.im, s.re, s.im)) { | ... | @@ -9,13 +9,12 @@ pub fn pow(z: anytype, s: anytype) Complex(@TypeOf(z.re, z.im, s.re, s.im)) { |
| 9 | return cmath.exp(cmath.log(z).mul(s)); | 9 | return cmath.exp(cmath.log(z).mul(s)); |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | const epsilon = 0.0001; | ||
| 13 | |||
| 14 | test pow { | 12 | test pow { |
| 13 | const epsilon = math.floatEps(f32); | ||
| 15 | const a = Complex(f32).init(5, 3); | 14 | const a = Complex(f32).init(5, 3); |
| 16 | const b = Complex(f32).init(2.3, -1.3); | 15 | const b = Complex(f32).init(2.3, -1.3); |
| 17 | const c = pow(a, b); | 16 | const c = pow(a, b); |
| 18 | 17 | ||
| 19 | try testing.expect(math.approxEqAbs(f32, c.re, 58.049110, epsilon)); | 18 | try testing.expectApproxEqAbs(58.049110, c.re, epsilon); |
| 20 | try testing.expect(math.approxEqAbs(f32, c.im, -101.003433, epsilon)); | 19 | try testing.expectApproxEqAbs(-101.003433, c.im, epsilon); |
| 21 | } | 20 | } |
lib/std/math/complex/proj.zig+2-1| ... | @@ -19,5 +19,6 @@ test proj { | ... | @@ -19,5 +19,6 @@ test proj { |
| 19 | const a = Complex(f32).init(5, 3); | 19 | const a = Complex(f32).init(5, 3); |
| 20 | const c = proj(a); | 20 | const c = proj(a); |
| 21 | 21 | ||
| 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); | ||
| 23 | } | 24 | } |
lib/std/math/complex/sin.zig+3-4| ... | @@ -12,12 +12,11 @@ pub fn sin(z: anytype) Complex(@TypeOf(z.re, z.im)) { | ... | @@ -12,12 +12,11 @@ pub fn sin(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 12 | return Complex(T).init(q.im, -q.re); | 12 | return Complex(T).init(q.im, -q.re); |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | const epsilon = 0.0001; | ||
| 16 | |||
| 17 | test sin { | 15 | test sin { |
| 16 | const epsilon = math.floatEps(f32); | ||
| 18 | const a = Complex(f32).init(5, 3); | 17 | const a = Complex(f32).init(5, 3); |
| 19 | const c = sin(a); | 18 | const c = sin(a); |
| 20 | 19 | ||
| 21 | try testing.expect(math.approxEqAbs(f32, c.re, -9.654126, epsilon)); | 20 | try testing.expectApproxEqAbs(-9.654126, c.re, epsilon); |
| 22 | try testing.expect(math.approxEqAbs(f32, c.im, 2.841692, epsilon)); | 21 | try testing.expectApproxEqAbs(2.8416924, c.im, epsilon); |
| 23 | } | 22 | } |
lib/std/math/complex/sinh.zig+6-6| ... | @@ -152,20 +152,20 @@ fn sinh64(z: Complex(f64)) Complex(f64) { | ... | @@ -152,20 +152,20 @@ fn sinh64(z: Complex(f64)) Complex(f64) { |
| 152 | return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y)); | 152 | return Complex(f64).init((x * x) * (y - y), (x + x) * (y - y)); |
| 153 | } | 153 | } |
| 154 | 154 | ||
| 155 | const epsilon = 0.0001; | ||
| 156 | |||
| 157 | test sinh32 { | 155 | test sinh32 { |
| 156 | const epsilon = math.floatEps(f32); | ||
| 158 | const a = Complex(f32).init(5, 3); | 157 | const a = Complex(f32).init(5, 3); |
| 159 | const c = sinh(a); | 158 | const c = sinh(a); |
| 160 | 159 | ||
| 161 | try testing.expect(math.approxEqAbs(f32, c.re, -73.460617, epsilon)); | 160 | try testing.expectApproxEqAbs(-73.460617, c.re, epsilon); |
| 162 | try testing.expect(math.approxEqAbs(f32, c.im, 10.472508, epsilon)); | 161 | try testing.expectApproxEqAbs(10.472508, c.im, epsilon); |
| 163 | } | 162 | } |
| 164 | 163 | ||
| 165 | test sinh64 { | 164 | test sinh64 { |
| 165 | const epsilon = math.floatEps(f64); | ||
| 166 | const a = Complex(f64).init(5, 3); | 166 | const a = Complex(f64).init(5, 3); |
| 167 | const c = sinh(a); | 167 | const c = sinh(a); |
| 168 | 168 | ||
| 169 | try testing.expect(math.approxEqAbs(f64, c.re, -73.460617, epsilon)); | 169 | try testing.expectApproxEqAbs(-73.46062169567367, c.re, epsilon); |
| 170 | try testing.expect(math.approxEqAbs(f64, c.im, 10.472508, epsilon)); | 170 | try testing.expectApproxEqAbs(10.472508533940392, c.im, epsilon); |
| 171 | } | 171 | } |
lib/std/math/complex/sqrt.zig+8-8| ... | @@ -43,7 +43,7 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { | ... | @@ -43,7 +43,7 @@ fn sqrt32(z: Complex(f32)) Complex(f32) { |
| 43 | // sqrt(-inf + i nan) = nan +- inf i | 43 | // sqrt(-inf + i nan) = nan +- inf i |
| 44 | // sqrt(-inf + iy) = 0 + inf i | 44 | // sqrt(-inf + iy) = 0 + inf i |
| 45 | if (math.signbit(x)) { | 45 | if (math.signbit(x)) { |
| 46 | return Complex(f32).init(@abs(x - y), math.copysign(x, y)); | 46 | return Complex(f32).init(@abs(y - y), math.copysign(x, y)); |
| 47 | } else { | 47 | } else { |
| 48 | return Complex(f32).init(x, math.copysign(y - y, y)); | 48 | return Complex(f32).init(x, math.copysign(y - y, y)); |
| 49 | } | 49 | } |
| ... | @@ -94,7 +94,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { | ... | @@ -94,7 +94,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { |
| 94 | // sqrt(-inf + i nan) = nan +- inf i | 94 | // sqrt(-inf + i nan) = nan +- inf i |
| 95 | // sqrt(-inf + iy) = 0 + inf i | 95 | // sqrt(-inf + iy) = 0 + inf i |
| 96 | if (math.signbit(x)) { | 96 | if (math.signbit(x)) { |
| 97 | return Complex(f64).init(@abs(x - y), math.copysign(x, y)); | 97 | return Complex(f64).init(@abs(y - y), math.copysign(x, y)); |
| 98 | } else { | 98 | } else { |
| 99 | return Complex(f64).init(x, math.copysign(y - y, y)); | 99 | return Complex(f64).init(x, math.copysign(y - y, y)); |
| 100 | } | 100 | } |
| ... | @@ -127,20 +127,20 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { | ... | @@ -127,20 +127,20 @@ fn sqrt64(z: Complex(f64)) Complex(f64) { |
| 127 | return result; | 127 | return result; |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | const epsilon = 0.0001; | ||
| 131 | |||
| 132 | test sqrt32 { | 130 | test sqrt32 { |
| 131 | const epsilon = math.floatEps(f32); | ||
| 133 | const a = Complex(f32).init(5, 3); | 132 | const a = Complex(f32).init(5, 3); |
| 134 | const c = sqrt(a); | 133 | const c = sqrt(a); |
| 135 | 134 | ||
| 136 | try testing.expect(math.approxEqAbs(f32, c.re, 2.327117, epsilon)); | 135 | try testing.expectApproxEqAbs(2.3271174, c.re, epsilon); |
| 137 | try testing.expect(math.approxEqAbs(f32, c.im, 0.644574, epsilon)); | 136 | try testing.expectApproxEqAbs(0.6445742, c.im, epsilon); |
| 138 | } | 137 | } |
| 139 | 138 | ||
| 140 | test sqrt64 { | 139 | test sqrt64 { |
| 140 | const epsilon = math.floatEps(f64); | ||
| 141 | const a = Complex(f64).init(5, 3); | 141 | const a = Complex(f64).init(5, 3); |
| 142 | const c = sqrt(a); | 142 | const c = sqrt(a); |
| 143 | 143 | ||
| 144 | try testing.expect(math.approxEqAbs(f64, c.re, 2.3271175190399496, epsilon)); | 144 | try testing.expectApproxEqAbs(2.3271175190399496, c.re, epsilon); |
| 145 | try testing.expect(math.approxEqAbs(f64, c.im, 0.6445742373246469, epsilon)); | 145 | try testing.expectApproxEqAbs(0.6445742373246469, c.im, epsilon); |
| 146 | } | 146 | } |
lib/std/math/complex/tan.zig+3-4| ... | @@ -12,12 +12,11 @@ pub fn tan(z: anytype) Complex(@TypeOf(z.re, z.im)) { | ... | @@ -12,12 +12,11 @@ pub fn tan(z: anytype) Complex(@TypeOf(z.re, z.im)) { |
| 12 | return Complex(T).init(r.im, -r.re); | 12 | return Complex(T).init(r.im, -r.re); |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | const epsilon = 0.0001; | ||
| 16 | |||
| 17 | test tan { | 15 | test tan { |
| 16 | const epsilon = math.floatEps(f32); | ||
| 18 | const a = Complex(f32).init(5, 3); | 17 | const a = Complex(f32).init(5, 3); |
| 19 | const c = tan(a); | 18 | const c = tan(a); |
| 20 | 19 | ||
| 21 | try testing.expect(math.approxEqAbs(f32, c.re, -0.002708233, epsilon)); | 20 | try testing.expectApproxEqAbs(-0.002708233, c.re, epsilon); |
| 22 | try testing.expect(math.approxEqAbs(f32, c.im, 1.004165, epsilon)); | 21 | try testing.expectApproxEqAbs(1.0041647, c.im, epsilon); |
| 23 | } | 22 | } |
lib/std/math/complex/tanh.zig+16-7| ... | @@ -70,7 +70,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) { | ... | @@ -70,7 +70,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) { |
| 70 | const ix = hx & 0x7fffffff; | 70 | const ix = hx & 0x7fffffff; |
| 71 | 71 | ||
| 72 | if (ix >= 0x7ff00000) { | 72 | if (ix >= 0x7ff00000) { |
| 73 | if ((ix & 0x7fffff) | lx != 0) { | 73 | if ((ix & 0xfffff) | lx != 0) { |
| 74 | const r = if (y == 0) y else x * y; | 74 | const r = if (y == 0) y else x * y; |
| 75 | return Complex(f64).init(x, r); | 75 | return Complex(f64).init(x, r); |
| 76 | } | 76 | } |
| ... | @@ -101,20 +101,29 @@ fn tanh64(z: Complex(f64)) Complex(f64) { | ... | @@ -101,20 +101,29 @@ fn tanh64(z: Complex(f64)) Complex(f64) { |
| 101 | return Complex(f64).init((beta * rho * s) / den, t / den); | 101 | return Complex(f64).init((beta * rho * s) / den, t / den); |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | const epsilon = 0.0001; | ||
| 105 | |||
| 106 | test tanh32 { | 104 | test tanh32 { |
| 105 | const epsilon = math.floatEps(f32); | ||
| 107 | const a = Complex(f32).init(5, 3); | 106 | const a = Complex(f32).init(5, 3); |
| 108 | const c = tanh(a); | 107 | const c = tanh(a); |
| 109 | 108 | ||
| 110 | try testing.expect(math.approxEqAbs(f32, c.re, 0.999913, epsilon)); | 109 | try testing.expectApproxEqAbs(0.99991274, c.re, epsilon); |
| 111 | try testing.expect(math.approxEqAbs(f32, c.im, -0.000025, epsilon)); | 110 | try testing.expectApproxEqAbs(-0.00002536878, c.im, epsilon); |
| 112 | } | 111 | } |
| 113 | 112 | ||
| 114 | test tanh64 { | 113 | test tanh64 { |
| 114 | const epsilon = math.floatEps(f64); | ||
| 115 | const a = Complex(f64).init(5, 3); | 115 | const a = Complex(f64).init(5, 3); |
| 116 | const c = tanh(a); | 116 | const c = tanh(a); |
| 117 | 117 | ||
| 118 | try testing.expect(math.approxEqAbs(f64, c.re, 0.999913, epsilon)); | 118 | try testing.expectApproxEqAbs(0.9999128201513536, c.re, epsilon); |
| 119 | try testing.expect(math.approxEqAbs(f64, c.im, -0.000025, epsilon)); | 119 | try testing.expectApproxEqAbs(-0.00002536867620767604, c.im, epsilon); |
| 120 | } | ||
| 121 | |||
| 122 | test "tanh64 musl" { | ||
| 123 | const epsilon = math.floatEps(f64); | ||
| 124 | const a = Complex(f64).init(std.math.inf(f64), std.math.inf(f64)); | ||
| 125 | const c = tanh(a); | ||
| 126 | |||
| 127 | try testing.expectApproxEqAbs(1, c.re, epsilon); | ||
| 128 | try testing.expectApproxEqAbs(0, c.im, epsilon); | ||
| 120 | } | 129 | } |