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;
1010
1111/// Returns the arc-tangent of y/x.
1212///
13/// Special Cases:
14/// - atan2(y, nan) = nan
15/// - atan2(nan, x) = nan
16/// - atan2(+0, x>=0) = +0
17/// - atan2(-0, x>=0) = -0
18/// - atan2(+0, x<=-0) = +pi
19/// - atan2(-0, x<=-0) = -pi
20/// - atan2(y>0, 0) = +pi/2
21/// - atan2(y<0, 0) = -pi/2
22/// - atan2(+inf, +inf) = +pi/4
23/// - atan2(-inf, +inf) = -pi/4
24/// - atan2(+inf, -inf) = 3pi/4
25/// - atan2(-inf, -inf) = -3pi/4
26/// - atan2(y, +inf) = 0
27/// - atan2(y>0, -inf) = +pi
28/// - atan2(y<0, -inf) = -pi
29/// - atan2(+inf, x) = +pi/2
30/// - atan2(-inf, x) = -pi/2
31pub fn atan2(comptime T: type, y: T, x: T) T {
13/// Special Cases:
14/// | y | x | radians |
15/// |-------|-------|---------|
16/// | fin | nan | nan |
17/// | nan | fin | nan |
18/// | +0 | >=+0 | +0 |
19/// | -0 | >=+0 | -0 |
20/// | +0 | <=-0 | pi |
21/// | -0 | <=-0 | -pi |
22/// | pos | 0 | +pi/2 |
23/// | neg | 0 | -pi/2 |
24/// | +inf | +inf | +pi/4 |
25/// | -inf | +inf | -pi/4 |
26/// | +inf | -inf | 3pi/4 |
27/// | -inf | -inf | -3pi/4 |
28/// | fin | +inf | 0 |
29/// | pos | -inf | +pi |
30/// | neg | -inf | -pi |
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);
3235 return switch (T) {
3336 f32 => atan2_32(y, x),
3437 f64 => atan2_64(y, x),
......@@ -212,8 +215,12 @@ fn atan2_64(y: f64, x: f64) f64 {
212215}
213216
214217test "math.atan2" {
215 try expect(atan2(f32, 0.2, 0.21) == atan2_32(0.2, 0.21));
216 try expect(atan2(f64, 0.2, 0.21) == atan2_64(0.2, 0.21));
218 const y32: f32 = 0.2;
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));
217224}
218225
219226test "math.atan2_32" {
lib/std/math/complex/arg.zig+2-3
......@@ -5,9 +5,8 @@ const cmath = math.complex;
55const Complex = cmath.Complex;
66
77/// Returns the angular component (in radians) of z.
8pub fn arg(z: anytype) @TypeOf(z.re) {
9 const T = @TypeOf(z.re);
10 return math.atan2(T, z.im, z.re);
8pub fn arg(z: anytype) @TypeOf(z.re, z.im) {
9 return math.atan2(z.im, z.re);
1110}
1211
1312const epsilon = 0.0001;
lib/std/math/complex/atan.zig+2-2
......@@ -54,7 +54,7 @@ fn atan32(z: Complex(f32)) Complex(f32) {
5454 return Complex(f32).init(maxnum, maxnum);
5555 }
5656
57 var t = 0.5 * math.atan2(f32, 2.0 * x, a);
57 var t = 0.5 * math.atan2(2.0 * x, a);
5858 const w = redupif32(t);
5959
6060 t = y - 1.0;
......@@ -103,7 +103,7 @@ fn atan64(z: Complex(f64)) Complex(f64) {
103103 return Complex(f64).init(maxnum, maxnum);
104104 }
105105
106 var t = 0.5 * math.atan2(f64, 2.0 * x, a);
106 var t = 0.5 * math.atan2(2.0 * x, a);
107107 const w = redupif64(t);
108108
109109 t = y - 1.0;