Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

atan2 in std #141

Closed
sciconf opened this issue Nov 26, 2023 · 2 comments
Closed

atan2 in std #141

sciconf opened this issue Nov 26, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@sciconf
Copy link

sciconf commented Nov 26, 2023

It would be great to add atan2 to std. atan2 is super useful for converting from rectangular to polar coordinates.

I think it looks something like this, but ... I'm not expert with rust at all:

--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -104,6 +104,7 @@ pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
                ("asin", builtin_asin::INST),
                ("acos", builtin_acos::INST),
                ("atan", builtin_atan::INST),
+               ("atan2", builtin_atan2::INST),
--- a/crates/jrsonnet-stdlib/src/math.rs
+++ b/crates/jrsonnet-stdlib/src/math.rs
@@ -89,6 +89,11 @@ pub fn builtin_atan(x: f64) -> f64 {
        x.atan()
 }

+#[builtin]
+pub fn builtin_atan2(y:f64, x: f64) -> f64 {
+       y.atan2(x)
+}
+

@CertainLach CertainLach added the enhancement New feature or request label Nov 26, 2023
@CertainLach
Copy link
Owner

CertainLach commented Nov 26, 2023

I agree this is useful, and yes, your code is correct for that.
I have such helper in one of my projects using jsonnet for now:

local atan2(y, x) =
  if x > 0 then
    atan(y / x)
  else if x < 0 && y >= 0 then
    atan(y / x) + pi
  else if x < 0 && y < 0 then
    atan(y / x) - pi
  else if x == 0 && y > 0 then
    pi / 2
  else if x == 0 && y < 0 then
    -pi / 2
  else
    error "atan2(0, 0) is undefined"; 

But I agree this is acceptable to add this to standard library. The problem is, it should also be added to official jsonnet implementation...

@CertainLach CertainLach self-assigned this Nov 26, 2023
@sciconf
Copy link
Author

sciconf commented Nov 26, 2023

I also have nearly the same jsonnet atan2 code myself too, but it just seems better to be in std with all the other trig. I put a request in for the c++ library as well:
google/jsonnet#1118
But, since jrsonnet is way faster and what I actually use, this is where I'd really like it :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants