authorgravatar for 77922942+expikr@users.noreply.github.comexpikr <77922942+expikr@users.noreply.github.com> 2024-01-15 10:04:30+08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-01-14 21:04:30-05:00
logf9d8176e94cf355dc5c4c35fbdd7e125eb29bcef
tree091725b893aae5e3b2d17701697041e423551496
parent1a7a711964c17b2e2b0ab5f404898bb729012db1
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Update atan2.zig (#17840)

Co-authored-by: castholm <carl@astholm.se>

3 files changed, 32 insertions(+), 26 deletions(-)

lib/std/math/atan2.zig+28-21
...@@ -10,25 +10,28 @@ const expect = std.testing.expect;...@@ -10,25 +10,28 @@ const expect = std.testing.expect;
1010
11/// Returns the arc-tangent of y/x.11/// Returns the arc-tangent of y/x.
12///12///
13/// Special Cases:13/// Special Cases:
14/// - atan2(y, nan) = nan14/// | y | x | radians |
15/// - atan2(nan, x) = nan15/// |-------|-------|---------|
16/// - atan2(+0, x>=0) = +016/// | fin | nan | nan |
17/// - atan2(-0, x>=0) = -017/// | nan | fin | nan |
18/// - atan2(+0, x<=-0) = +pi18/// | +0 | >=+0 | +0 |
19/// - atan2(-0, x<=-0) = -pi19/// | -0 | >=+0 | -0 |
20/// - atan2(y>0, 0) = +pi/220/// | +0 | <=-0 | pi |
21/// - atan2(y<0, 0) = -pi/221/// | -0 | <=-0 | -pi |
22/// - atan2(+inf, +inf) = +pi/422/// | pos | 0 | +pi/2 |
23/// - atan2(-inf, +inf) = -pi/423/// | neg | 0 | -pi/2 |
24/// - atan2(+inf, -inf) = 3pi/424/// | +inf | +inf | +pi/4 |
25/// - atan2(-inf, -inf) = -3pi/425/// | -inf | +inf | -pi/4 |
26/// - atan2(y, +inf) = 026/// | +inf | -inf | 3pi/4 |
27/// - atan2(y>0, -inf) = +pi27/// | -inf | -inf | -3pi/4 |
28/// - atan2(y<0, -inf) = -pi28/// | fin | +inf | 0 |
29/// - atan2(+inf, x) = +pi/229/// | pos | -inf | +pi |
30/// - atan2(-inf, x) = -pi/230/// | neg | -inf | -pi |
31pub fn atan2(comptime T: type, y: T, x: T) T {31/// | +inf | fin | +pi/2 |
32/// | -inf | fin | -pi/2 |
33pub fn atan2(y: anytype, x: anytype) @TypeOf(x, y) {
34 const T = @TypeOf(x, y);
32 return switch (T) {35 return switch (T) {
33 f32 => atan2_32(y, x),36 f32 => atan2_32(y, x),
34 f64 => atan2_64(y, x),37 f64 => atan2_64(y, x),
...@@ -212,8 +215,12 @@ fn atan2_64(y: f64, x: f64) f64 {...@@ -212,8 +215,12 @@ fn atan2_64(y: f64, x: f64) f64 {
212}215}
213216
214test "math.atan2" {217test "math.atan2" {
215 try expect(atan2(f32, 0.2, 0.21) == atan2_32(0.2, 0.21));218 const y32: f32 = 0.2;
216 try expect(atan2(f64, 0.2, 0.21) == atan2_64(0.2, 0.21));219 const x32: f32 = 0.21;
220 const y64: f64 = 0.2;
221 const x64: f64 = 0.21;
222 try expect(atan2(y32, x32) == atan2_32(0.2, 0.21));
223 try expect(atan2(y64, x64) == atan2_64(0.2, 0.21));
217}224}
218225
219test "math.atan2_32" {226test "math.atan2_32" {
lib/std/math/complex/arg.zig+2-3
...@@ -5,9 +5,8 @@ const cmath = math.complex;...@@ -5,9 +5,8 @@ const cmath = math.complex;
5const Complex = cmath.Complex;5const Complex = cmath.Complex;
66
7/// Returns the angular component (in radians) of z.7/// Returns the angular component (in radians) of z.
8pub fn arg(z: anytype) @TypeOf(z.re) {8pub fn arg(z: anytype) @TypeOf(z.re, z.im) {
9 const T = @TypeOf(z.re);9 return math.atan2(z.im, z.re);
10 return math.atan2(T, z.im, z.re);
11}10}
1211
13const epsilon = 0.0001;12const epsilon = 0.0001;
lib/std/math/complex/atan.zig+2-2
...@@ -54,7 +54,7 @@ fn atan32(z: Complex(f32)) Complex(f32) {...@@ -54,7 +54,7 @@ fn atan32(z: Complex(f32)) Complex(f32) {
54 return Complex(f32).init(maxnum, maxnum);54 return Complex(f32).init(maxnum, maxnum);
55 }55 }
5656
57 var t = 0.5 * math.atan2(f32, 2.0 * x, a);57 var t = 0.5 * math.atan2(2.0 * x, a);
58 const w = redupif32(t);58 const w = redupif32(t);
5959
60 t = y - 1.0;60 t = y - 1.0;
...@@ -103,7 +103,7 @@ fn atan64(z: Complex(f64)) Complex(f64) {...@@ -103,7 +103,7 @@ fn atan64(z: Complex(f64)) Complex(f64) {
103 return Complex(f64).init(maxnum, maxnum);103 return Complex(f64).init(maxnum, maxnum);
104 }104 }
105105
106 var t = 0.5 * math.atan2(f64, 2.0 * x, a);106 var t = 0.5 * math.atan2(2.0 * x, a);
107 const w = redupif64(t);107 const w = redupif64(t);
108108
109 t = y - 1.0;109 t = y - 1.0;