Commit f9d8176e94

expikr <77922942+expikr@users.noreply.github.com>
2024-01-15 03:04:30
Update atan2.zig (#17840)
Co-authored-by: castholm <carl@astholm.se>
1 parent 1a7a711
Changed files (3)
lib
std
lib/std/math/complex/arg.zig
@@ -5,9 +5,8 @@ const cmath = math.complex;
 const Complex = cmath.Complex;
 
 /// Returns the angular component (in radians) of z.
-pub fn arg(z: anytype) @TypeOf(z.re) {
-    const T = @TypeOf(z.re);
-    return math.atan2(T, z.im, z.re);
+pub fn arg(z: anytype) @TypeOf(z.re, z.im) {
+    return math.atan2(z.im, z.re);
 }
 
 const epsilon = 0.0001;
lib/std/math/complex/atan.zig
@@ -54,7 +54,7 @@ fn atan32(z: Complex(f32)) Complex(f32) {
         return Complex(f32).init(maxnum, maxnum);
     }
 
-    var t = 0.5 * math.atan2(f32, 2.0 * x, a);
+    var t = 0.5 * math.atan2(2.0 * x, a);
     const w = redupif32(t);
 
     t = y - 1.0;
@@ -103,7 +103,7 @@ fn atan64(z: Complex(f64)) Complex(f64) {
         return Complex(f64).init(maxnum, maxnum);
     }
 
-    var t = 0.5 * math.atan2(f64, 2.0 * x, a);
+    var t = 0.5 * math.atan2(2.0 * x, a);
     const w = redupif64(t);
 
     t = y - 1.0;
lib/std/math/atan2.zig
@@ -10,25 +10,28 @@ const expect = std.testing.expect;
 
 /// Returns the arc-tangent of y/x.
 ///
-/// Special Cases:
-///  - atan2(y, nan)     = nan
-///  - atan2(nan, x)     = nan
-///  - atan2(+0, x>=0)   = +0
-///  - atan2(-0, x>=0)   = -0
-///  - atan2(+0, x<=-0)  = +pi
-///  - atan2(-0, x<=-0)  = -pi
-///  - atan2(y>0, 0)     = +pi/2
-///  - atan2(y<0, 0)     = -pi/2
-///  - atan2(+inf, +inf) = +pi/4
-///  - atan2(-inf, +inf) = -pi/4
-///  - atan2(+inf, -inf) = 3pi/4
-///  - atan2(-inf, -inf) = -3pi/4
-///  - atan2(y, +inf)    = 0
-///  - atan2(y>0, -inf)  = +pi
-///  - atan2(y<0, -inf)  = -pi
-///  - atan2(+inf, x)    = +pi/2
-///  - atan2(-inf, x)    = -pi/2
-pub fn atan2(comptime T: type, y: T, x: T) T {
+///      Special Cases:
+/// |   y   |   x   | radians |
+/// |-------|-------|---------|
+/// |  fin  |  nan  |   nan   |
+/// |  nan  |  fin  |   nan   |
+/// |  +0   | >=+0  |   +0    |
+/// |  -0   | >=+0  |   -0    |
+/// |  +0   | <=-0  |   pi    |
+/// |  -0   | <=-0  |  -pi    |
+/// |  pos  |   0   |  +pi/2  |
+/// |  neg  |   0   |  -pi/2  |
+/// | +inf  | +inf  |  +pi/4  |
+/// | -inf  | +inf  |  -pi/4  |
+/// | +inf  | -inf  |  3pi/4  |
+/// | -inf  | -inf  | -3pi/4  |
+/// |  fin  | +inf  |    0    |
+/// |  pos  | -inf  |  +pi    |
+/// |  neg  | -inf  |  -pi    |
+/// | +inf  |  fin  |  +pi/2  |
+/// | -inf  |  fin  |  -pi/2  |
+pub fn atan2(y: anytype, x: anytype) @TypeOf(x, y) {
+    const T = @TypeOf(x, y);
     return switch (T) {
         f32 => atan2_32(y, x),
         f64 => atan2_64(y, x),
@@ -212,8 +215,12 @@ fn atan2_64(y: f64, x: f64) f64 {
 }
 
 test "math.atan2" {
-    try expect(atan2(f32, 0.2, 0.21) == atan2_32(0.2, 0.21));
-    try expect(atan2(f64, 0.2, 0.21) == atan2_64(0.2, 0.21));
+    const y32: f32 = 0.2;
+    const x32: f32 = 0.21;
+    const y64: f64 = 0.2;
+    const x64: f64 = 0.21;
+    try expect(atan2(y32, x32) == atan2_32(0.2, 0.21));
+    try expect(atan2(y64, x64) == atan2_64(0.2, 0.21));
 }
 
 test "math.atan2_32" {