diff --git a/pkl-core/src/main/java/org/pkl/core/stdlib/math/MathNodes.java b/pkl-core/src/main/java/org/pkl/core/stdlib/math/MathNodes.java index a2160b78..92735383 100644 --- a/pkl-core/src/main/java/org/pkl/core/stdlib/math/MathNodes.java +++ b/pkl-core/src/main/java/org/pkl/core/stdlib/math/MathNodes.java @@ -299,6 +299,28 @@ public final class MathNodes { } } + public abstract static class atan2 extends ExternalMethod2Node { + @Specialization + protected double eval(VmTyped self, long x, long y) { + return StrictMath.atan2(x, y); + } + + @Specialization + protected double eval(VmTyped self, long x, double y) { + return StrictMath.atan2(x, y); + } + + @Specialization + protected double eval(VmTyped self, double x, double y) { + return StrictMath.atan2(x, y); + } + + @Specialization + protected double eval(VmTyped self, double x, long y) { + return StrictMath.atan2(x, y); + } + } + public abstract static class gcd extends ExternalMethod2Node { @TruffleBoundary @Specialization diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input/api/mathModule.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input/api/mathModule.pkl index 96045d72..f8fa5d8c 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/input/api/mathModule.pkl +++ b/pkl-core/src/test/files/LanguageSnippetTests/input/api/mathModule.pkl @@ -130,6 +130,13 @@ examples { math.atan(math.tan(-2.34)) } + ["atan2"] { + math.atan2(4, -3) + math.atan2(4.5, -3.5) + math.atan2(4.5, -3) + math.atan2(4, 3.5) + } + ["gcd"] { math.gcd(0, 0) math.gcd(4, 6) @@ -189,3 +196,10 @@ examples { math.min(Infinity, -Infinity) } } + +facts { + ["atan2"] { + math.atan2(1, 0) == math.pi / 2 + math.atan2(0, -1) == math.pi + } +} diff --git a/pkl-core/src/test/files/LanguageSnippetTests/output/api/mathModule.pcf b/pkl-core/src/test/files/LanguageSnippetTests/output/api/mathModule.pcf index ca33fd10..5fe54ea2 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/output/api/mathModule.pcf +++ b/pkl-core/src/test/files/LanguageSnippetTests/output/api/mathModule.pcf @@ -1,3 +1,9 @@ +facts { + ["atan2"] { + true + true + } +} examples { ["minInt"] { -9223372036854775808 @@ -109,6 +115,12 @@ examples { 1.1415926535897931 0.8015926535897934 } + ["atan2"] { + 2.214297435588181 + 2.2318394956455836 + 2.1587989303424644 + 0.851966327173272 + } ["gcd"] { 0 2 diff --git a/stdlib/math.pkl b/stdlib/math.pkl index dfdeb42f..cea12a85 100644 --- a/stdlib/math.pkl +++ b/stdlib/math.pkl @@ -145,6 +145,12 @@ external function acos(x: Number): Float /// [1]: https://en.wikipedia.org/wiki/Inverse_trigonometric_functions external function atan(x: Number): Float +/// Returns the [2-argument arctangent][1] of [x] and [y]. +/// +/// [1]: https://en.wikipedia.org/wiki/Atan2 +@Since { version = "0.28.0" } +external function atan2(x: Number, y: Number): Float + /// Returns the [greatest common divisor][1] of [x] and [y]. /// /// [1]: https://en.wikipedia.org/wiki/Greatest_common_divisor