Skip to content

Commit 145b783

Browse files
authored
feat(stdlib): Add atan2 to the Number module (#2016)
1 parent f3bb13f commit 145b783

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

compiler/test/stdlib/number.test.gr

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,35 @@ assert Number.isClose(
808808
)
809809
assert Number.isNaN(Number.atan(NaN))
810810

811+
// Number.atan2
812+
assert Number.atan2(-8.06684839057968084, 4.53566256067686879) ==
813+
-1.0585895402489023
814+
assert Number.atan2(4.34523984933830487, -8.88799136300345083) ==
815+
2.6868734126013065
816+
assert Number.atan2(-8.38143342755524934, -2.76360733737958819) ==
817+
-1.88930009184952796
818+
assert Number.atan2(-6.53167358191348413, 4.56753527684274374) ==
819+
-0.960546902111148904
820+
assert Number.atan2(9.26705696697258574, 4.81139208435979615) ==
821+
1.09191239461421086
822+
assert Number.atan2(-6.45004555606023633, 0.662071792337673881) ==
823+
-1.4685085006164239
824+
assert Number.atan2(7.85889025304169664, 0.0521545267500622481) ==
825+
1.5641600512601266
826+
assert Number.atan2(-0.792054511984895959, 7.67640268511753998) ==
827+
-0.102816589106785081
828+
assert Number.atan2(0.615702673197924044, 2.01190257903248026) ==
829+
0.2969797400449351
830+
assert Number.atan2(-0.558758682360915193, 0.0322398306026380407) ==
831+
-1.51316120533039156
832+
assert Number.atan2(1, 0) == Number.pi / 2
833+
assert Number.atan2(-1, 0) == Number.pi / -2
834+
assert Number.atan2(0, 0) == 0
835+
assert Number.atan2(0, -1) == Number.pi
836+
assert Number.atan2(0, -Infinity) == Number.pi
837+
assert Number.atan2(0, 1) == 0
838+
assert Number.atan2(0, Infinity) == 0
839+
811840
// Number.toDegrees
812841
assert Number.toDegrees(0) == 0
813842
assert Number.toDegrees(Number.pi) == 180

stdlib/number.gr

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,33 @@ provide let atan = angle => {
795795
return WasmI32.toGrain(newFloat64(WasmF64.copySign(z, sx))): Number
796796
}
797797

798+
/**
799+
* Computes the angle between the positive x-axis and the ray from the origin to the point (x, y).
800+
*
801+
* @param y: The given y coordinate
802+
* @param x: The given x coordinate
803+
* @returns The angle in radians between the positive x-axis and the point (x, y)
804+
*
805+
* @example Number.atan2(0, 1) == Number.pi
806+
*
807+
* @since v0.6.0
808+
*/
809+
provide let atan2 = (y, x) => {
810+
if (x > 0) {
811+
atan(y / x)
812+
} else if (x < 0 && y >= 0) {
813+
atan(y / x) + pi
814+
} else if (x < 0 && y < 0) {
815+
atan(y / x) - pi
816+
} else if (x == 0 && y > 0) {
817+
pi / 2
818+
} else if (x == 0 && y < 0) {
819+
pi / -2
820+
} else { // x == 0 && y == 0
821+
0
822+
}
823+
}
824+
798825
/**
799826
* Converts degrees to radians.
800827
*

stdlib/number.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,38 @@ Returns:
890890
|----|-----------|
891891
|`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`|
892892

893+
### Number.**atan2**
894+
895+
<details disabled>
896+
<summary tabindex="-1">Added in <code>next</code></summary>
897+
No other changes yet.
898+
</details>
899+
900+
```grain
901+
atan2 : (y: Number, x: Number) => Number
902+
```
903+
904+
Computes the angle between the positive x-axis and the ray from the origin to the point (x, y).
905+
906+
Parameters:
907+
908+
|param|type|description|
909+
|-----|----|-----------|
910+
|`y`|`Number`|The given y coordinate|
911+
|`x`|`Number`|The given x coordinate|
912+
913+
Returns:
914+
915+
|type|description|
916+
|----|-----------|
917+
|`Number`|The angle in radians between the positive x-axis and the point (x, y)|
918+
919+
Examples:
920+
921+
```grain
922+
Number.atan2(0, 1) == Number.pi
923+
```
924+
893925
### Number.**toRadians**
894926

895927
<details disabled>

0 commit comments

Comments
 (0)