From a22be2f4c41557f801d101ee8304c8d71cb01e55 Mon Sep 17 00:00:00 2001
From: Yaroslav Bolyukin <iam@lach.pw>
Date: Thu, 30 Nov 2023 22:04:26 +0100
Subject: [PATCH] feat: add std.atan2

Fixes: https://github.com/google/jsonnet/issues/1118
---
 doc/_stdlib_gen/stdlib-content.jsonnet |  1 +
 stdlib/std.jsonnet                     | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/doc/_stdlib_gen/stdlib-content.jsonnet b/doc/_stdlib_gen/stdlib-content.jsonnet
index 6dc301676..8a05210dd 100644
--- a/doc/_stdlib_gen/stdlib-content.jsonnet
+++ b/doc/_stdlib_gen/stdlib-content.jsonnet
@@ -109,6 +109,7 @@ local html = import 'html.libsonnet';
               <ul><code>std.asin(x)</code></ul>
               <ul><code>std.acos(x)</code></ul>
               <ul><code>std.atan(x)</code></ul>
+              <ul><code>std.atan2(y, x)</code></ul>
               <ul><code>std.round(x)</code></ul>
               <ul><code>std.isEven(x)</code></ul>
               <ul><code>std.isOdd(x)</code></ul>
diff --git a/stdlib/std.jsonnet b/stdlib/std.jsonnet
index a843d3a08..3cfeab986 100644
--- a/stdlib/std.jsonnet
+++ b/stdlib/std.jsonnet
@@ -1785,4 +1785,20 @@ limitations under the License.
   sha3(str):: go_only_function,
 
   trim(str):: std.stripChars(str, ' \t\n\f\r\u0085\u00A0'),
+
+  atan2(y, x)::
+    local pi = std.acos(-1);
+    if x > 0 then
+      std.atan(y / x)
+    else if x < 0 && y >= 0 then
+      std.atan(y / x) + pi
+    else if x < 0 && y < 0 then
+      std.atan(y / x) - pi
+    else if x == 0 && y > 0 then
+      pi / 2
+    else if x == 0 && y < 0 then
+      -pi / 2
+    else
+      0
+  ,
 }