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

std.math or builtin: a tan2(rad) = (y,x) function for tan with quadrant information preserved #18527

Open
expikr opened this issue Jan 12, 2024 · 3 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@expikr
Copy link
Contributor

expikr commented Jan 12, 2024

If

$$ \text{atan2}(y,x) = \theta $$

Then

$$ \text{tan2}(\theta) = (y,x) $$

Something like

pub fn tan2(rad: T) [2]T {
    return .{ @sin(rad) , @cos(rad) };
}
@mlugg
Copy link
Member

mlugg commented Jan 13, 2024

atan2 lives in the standard library because it is a complex algorithm and not suited for a builtin. What would the advantage be of having such a tan2 function which is just computing @sin and @cos? It's not even much longer to inline the definition:

std.math.tan2(rad)
// vs
.{ @sin(rad), @cos(rad) }

...and it's arguably much clearer.

@mlugg mlugg added standard library This issue involves writing Zig code for the standard library. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. labels Jan 13, 2024
@lifthrasiir
Copy link
Contributor

There is a potential speedup possible from calculating sin and cos together, and a number of math libraries do have sincos for this reason1. Given this precedent, any equivalent function should be named std.math.sincos.

Footnotes

  1. That made me wonder why atan2 is actually not asincos for a moment though ;-) Again, atan2 is also a very popular name for that particular function.

@Vexu
Copy link
Member

Vexu commented Jan 27, 2024

Zig's compiler-rt implements sincos so maybe it should be exposed as a builtin.

@Vexu Vexu added this to the 0.13.0 milestone Mar 26, 2024
@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

5 participants