authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2024-07-29 16:26:09+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2024-07-30 15:38:00+12:00
log2bd7af63d76d1c61bfd77a5402f94d105c375a1d
tree14f2fffdf23009c2a63fe2d4380d63c85c319eae
parentf219286573a7a1edafe3e1b5bc1e521a379ee2e2

std.math.complex: fix acosh/atan/cosh/sqrt

Some of these are upstream changes since the original port, others are translation errors.

4 files changed, 14 insertions(+), 40 deletions(-)

lib/std/math/complex/acosh.zig+5-1
......@@ -8,7 +8,11 @@ const Complex = cmath.Complex;
88pub fn acosh(z: anytype) Complex(@TypeOf(z.re, z.im)) {
99 const T = @TypeOf(z.re, z.im);
1010 const q = cmath.acos(z);
11 return Complex(T).init(-q.im, q.re);
11
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);
1216}
1317
1418const epsilon = 0.0001;
lib/std/math/complex/atan.zig+4-34
......@@ -32,37 +32,22 @@ fn redupif32(x: f32) f32 {
3232 t -= 0.5;
3333 }
3434
35 const u = @as(f32, @floatFromInt(@as(i32, @intFromFloat(t))));
36 return ((x - u * DP1) - u * DP2) - t * DP3;
35 const u: f32 = @trunc(t);
36 return ((x - u * DP1) - u * DP2) - u * DP3;
3737}
3838
3939fn atan32(z: Complex(f32)) Complex(f32) {
40 const maxnum = 1.0e38;
41
4240 const x = z.re;
4341 const y = z.im;
4442
45 if ((x == 0.0) and (y > 1.0)) {
46 // overflow
47 return Complex(f32).init(maxnum, maxnum);
48 }
49
5043 const x2 = x * x;
5144 var a = 1.0 - x2 - (y * y);
52 if (a == 0.0) {
53 // overflow
54 return Complex(f32).init(maxnum, maxnum);
55 }
5645
5746 var t = 0.5 * math.atan2(2.0 * x, a);
5847 const w = redupif32(t);
5948
6049 t = y - 1.0;
6150 a = x2 + t * t;
62 if (a == 0.0) {
63 // overflow
64 return Complex(f32).init(maxnum, maxnum);
65 }
6651
6752 t = y + 1.0;
6853 a = (x2 + (t * t)) / a;
......@@ -81,37 +66,22 @@ fn redupif64(x: f64) f64 {
8166 t -= 0.5;
8267 }
8368
84 const u = @as(f64, @floatFromInt(@as(i64, @intFromFloat(t))));
85 return ((x - u * DP1) - u * DP2) - t * DP3;
69 const u: f64 = @trunc(t);
70 return ((x - u * DP1) - u * DP2) - u * DP3;
8671}
8772
8873fn atan64(z: Complex(f64)) Complex(f64) {
89 const maxnum = 1.0e308;
90
9174 const x = z.re;
9275 const y = z.im;
9376
94 if ((x == 0.0) and (y > 1.0)) {
95 // overflow
96 return Complex(f64).init(maxnum, maxnum);
97 }
98
9977 const x2 = x * x;
10078 var a = 1.0 - x2 - (y * y);
101 if (a == 0.0) {
102 // overflow
103 return Complex(f64).init(maxnum, maxnum);
104 }
10579
10680 var t = 0.5 * math.atan2(2.0 * x, a);
10781 const w = redupif64(t);
10882
10983 t = y - 1.0;
11084 a = x2 + t * t;
111 if (a == 0.0) {
112 // overflow
113 return Complex(f64).init(maxnum, maxnum);
114 }
11585
11686 t = y + 1.0;
11787 a = (x2 + (t * t)) / a;
lib/std/math/complex/cosh.zig+3-3
......@@ -34,7 +34,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
3434
3535 if (ix < 0x7f800000 and iy < 0x7f800000) {
3636 if (iy == 0) {
37 return Complex(f32).init(math.cosh(x), y);
37 return Complex(f32).init(math.cosh(x), x * y);
3838 }
3939 // small x: normal case
4040 if (ix < 0x41100000) {
......@@ -45,7 +45,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
4545 if (ix < 0x42b17218) {
4646 // x < 88.7: exp(|x|) won't overflow
4747 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));
4949 }
5050 // x < 192.7: scale to avoid overflow
5151 else if (ix < 0x4340b1e7) {
......@@ -68,7 +68,7 @@ fn cosh32(z: Complex(f32)) Complex(f32) {
6868 if (hx & 0x7fffff == 0) {
6969 return Complex(f32).init(x * x, math.copysign(@as(f32, 0.0), x) * y);
7070 }
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));
7272 }
7373
7474 if (ix < 0x7f800000 and iy >= 0x7f800000) {
lib/std/math/complex/sqrt.zig+2-2
......@@ -43,7 +43,7 @@ fn sqrt32(z: Complex(f32)) Complex(f32) {
4343 // sqrt(-inf + i nan) = nan +- inf i
4444 // sqrt(-inf + iy) = 0 + inf i
4545 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));
4747 } else {
4848 return Complex(f32).init(x, math.copysign(y - y, y));
4949 }
......@@ -94,7 +94,7 @@ fn sqrt64(z: Complex(f64)) Complex(f64) {
9494 // sqrt(-inf + i nan) = nan +- inf i
9595 // sqrt(-inf + iy) = 0 + inf i
9696 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));
9898 } else {
9999 return Complex(f64).init(x, math.copysign(y - y, y));
100100 }