From b5723d7d44f3b5a735a27aeb5fe987ab4d4e98d9 Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Mon, 12 Feb 2024 22:36:20 -0500 Subject: [PATCH 1/3] feat(stdlib): Add `atan2` to the `Number` module --- compiler/test/stdlib/number.test.gr | 27 +++++++++++++++++++++++++++ stdlib/number.gr | 27 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/compiler/test/stdlib/number.test.gr b/compiler/test/stdlib/number.test.gr index 96a796f09..65289c9cc 100644 --- a/compiler/test/stdlib/number.test.gr +++ b/compiler/test/stdlib/number.test.gr @@ -808,6 +808,33 @@ assert Number.isClose( ) assert Number.isNaN(Number.atan(NaN)) +// Number.atan2 +assert Number.atan2(-8.06684839057968084, 4.53566256067686879) == + -1.0585895402489023 +assert Number.atan2(4.34523984933830487, -8.88799136300345083) == + 2.6868734126013065 +assert Number.atan2(-8.38143342755524934, -2.76360733737958819) == + -1.88930009184952796 +assert Number.atan2(-6.53167358191348413, 4.56753527684274374) == + -0.960546902111148904 +assert Number.atan2(9.26705696697258574, 4.81139208435979615) == + 1.09191239461421086 +assert Number.atan2(-6.45004555606023633, 0.662071792337673881) == + -1.4685085006164239 +assert Number.atan2(7.85889025304169664, 0.0521545267500622481) == + 1.5641600512601266 +assert Number.atan2(-0.792054511984895959, 7.67640268511753998) == + -0.102816589106785081 +assert Number.atan2(0.615702673197924044, 2.01190257903248026) == + 0.2969797400449351 +assert Number.atan2(-0.558758682360915193, 0.0322398306026380407) == + -1.51316120533039156 +assert Number.atan2(0, 0) == 0 +assert Number.atan2(0, -1) == Number.pi +assert Number.atan2(0, -Infinity) == Number.pi +assert Number.atan2(0, 1) == 0 +assert Number.atan2(0, Infinity) == 0 + // Number.toDegrees assert Number.toDegrees(0) == 0 assert Number.toDegrees(Number.pi) == 180 diff --git a/stdlib/number.gr b/stdlib/number.gr index 45efce961..c54e7ddfb 100644 --- a/stdlib/number.gr +++ b/stdlib/number.gr @@ -795,6 +795,33 @@ provide let atan = angle => { return WasmI32.toGrain(newFloat64(WasmF64.copySign(z, sx))): Number } +/** + * Computes the angle between the positive x-axis and the ray from the origin to the point (x, y). + * + * @param y: The given y coordinate + * @param x: The given x coordinate + * @returns The angle in radians between the positive x-axis and the point (x, y) + * + * @example Number.atan2(0, 1) == Number.pi + * + * @since v0.6.0 + */ +provide let atan2 = (y, x) => { + if (x > 0) { + atan(y / x) + } else if (x < 0 && y >= 0) { + atan(y / x) + pi + } else if (x < 0 && y < 0) { + atan(y / x) - pi + } else if (x == 0 && y > 0) { + 2 / pi + } else if (x == 0 && y < 0) { + -2 / pi + } else { // x == 0 && y == 0 + 0 + } +} + /** * Converts degrees to radians. * From 91000f0fb4af1c49948c386e5a936897279843f4 Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Sun, 18 Feb 2024 19:09:13 -0500 Subject: [PATCH 2/3] chore: Correct result --- compiler/test/stdlib/number.test.gr | 2 ++ stdlib/number.gr | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler/test/stdlib/number.test.gr b/compiler/test/stdlib/number.test.gr index 65289c9cc..90098b857 100644 --- a/compiler/test/stdlib/number.test.gr +++ b/compiler/test/stdlib/number.test.gr @@ -829,6 +829,8 @@ assert Number.atan2(0.615702673197924044, 2.01190257903248026) == 0.2969797400449351 assert Number.atan2(-0.558758682360915193, 0.0322398306026380407) == -1.51316120533039156 +assert Number.atan2(1, 0) == Number.pi / 2 +assert Number.atan2(-1, 0) == Number.pi / -2 assert Number.atan2(0, 0) == 0 assert Number.atan2(0, -1) == Number.pi assert Number.atan2(0, -Infinity) == Number.pi diff --git a/stdlib/number.gr b/stdlib/number.gr index c54e7ddfb..0cea3a0d6 100644 --- a/stdlib/number.gr +++ b/stdlib/number.gr @@ -814,9 +814,9 @@ provide let atan2 = (y, x) => { } else if (x < 0 && y < 0) { atan(y / x) - pi } else if (x == 0 && y > 0) { - 2 / pi + pi / 2 } else if (x == 0 && y < 0) { - -2 / pi + pi / -2 } else { // x == 0 && y == 0 0 } From 40196835d6a9508160cc74f20d5537e3309a8423 Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Tue, 27 Feb 2024 17:34:53 -0500 Subject: [PATCH 3/3] chore: Regen graindoc --- stdlib/number.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/stdlib/number.md b/stdlib/number.md index bcacc573a..c1307e442 100644 --- a/stdlib/number.md +++ b/stdlib/number.md @@ -890,6 +890,38 @@ Returns: |----|-----------| |`Number`|The inverse tangent (angle in radians between `-pi/2` and `pi/2`) of the given `angle` or `NaN` if the given `angle` is not between`-1` and `1`| +### Number.**atan2** + +
+Added in next +No other changes yet. +
+ +```grain +atan2 : (y: Number, x: Number) => Number +``` + +Computes the angle between the positive x-axis and the ray from the origin to the point (x, y). + +Parameters: + +|param|type|description| +|-----|----|-----------| +|`y`|`Number`|The given y coordinate| +|`x`|`Number`|The given x coordinate| + +Returns: + +|type|description| +|----|-----------| +|`Number`|The angle in radians between the positive x-axis and the point (x, y)| + +Examples: + +```grain +Number.atan2(0, 1) == Number.pi +``` + ### Number.**toRadians**