Skip to content

Commit 85c4389

Browse files
authored
chore(stdlib)!: Remove sin, cos, tan, gamma, factorial from Number module (#2046)
1 parent aead774 commit 85c4389

File tree

3 files changed

+0
-344
lines changed

3 files changed

+0
-344
lines changed

compiler/test/stdlib/number.test.gr

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -651,37 +651,6 @@ assert Number.sign(BI.toNumber(1t)) == 1
651651
assert 1 / Number.sign(0.0) == Infinity
652652
assert 1 / Number.sign(-0.0) == -Infinity
653653

654-
// Number.sin - These test a range as we approximate sin
655-
assert Number.sin(0.0) == 0
656-
assert Number.sin(1) > 0.84147 && Number.sin(1) < 0.84148
657-
assert Number.sin(-1) < -0.84147 && Number.sin(-1) > -0.84148
658-
assert Number.sin(20) > 0.91294 && Number.sin(20) < 0.91295
659-
assert Number.sin(1/2) > 0.4794 && Number.sin(1/2) < 0.4795
660-
// TODO(#1478): Update this test when sin is unbounded
661-
assert Number.sin(9223372036854775809) == 0.0
662-
assert Number.isNaN(Number.sin(Infinity))
663-
assert Number.isNaN(Number.sin(NaN))
664-
665-
// Number.cos - These test a range as we approximate sin
666-
assert Number.cos(1) > 0.5403 && Number.cos(1) < 0.5404
667-
assert Number.cos(-1) > 0.5403 && Number.cos(-1) < 0.5404
668-
assert Number.cos(15) < -0.7596 && Number.cos(15) > -0.7597
669-
assert Number.cos(1/2) > 0.8775 && Number.cos(1/2) < 0.8776
670-
// TODO(#1478): Update this test when sin is unbounded
671-
assert Number.cos(9223372036854775809) == 0.0
672-
assert Number.isNaN(Number.cos(Infinity))
673-
assert Number.isNaN(Number.cos(NaN))
674-
675-
// Number.tan - These test a range as we approximate sin
676-
assert Number.tan(1) > 1.5574 && Number.tan(1) < 1.5575
677-
assert Number.tan(-1) > -1.5575 && Number.tan(-1) < -1.5574
678-
assert Number.tan(15) < -0.8559 && Number.tan(15) > -0.8560
679-
assert Number.tan(1/2) > 0.5463 && Number.tan(1/2) < 0.5464
680-
// TODO(#1478): Update this test when sin is unbounded
681-
assert Number.isNaN(Number.tan(9223372036854775809))
682-
assert Number.isNaN(Number.tan(Infinity))
683-
assert Number.isNaN(Number.tan(NaN))
684-
685654
// Number.asin
686655
assert Number.isNaN(Number.asin(-8.06684839057968084))
687656
assert Number.isNaN(Number.asin(-1.1))
@@ -839,35 +808,6 @@ assert Number.isClose(
839808
)
840809
assert Number.isNaN(Number.atan(NaN))
841810

842-
// Number.gamma
843-
// Note: Currently gamma will overflow the memory when using a bigint as such there are no tests for this
844-
assert Number.gamma(10.5) < 1133278.3889488 &&
845-
Number.gamma(10.5) > 1133278.3889487
846-
assert Number.gamma(10) == 362880
847-
assert Number.gamma(1) == 1
848-
assert Number.gamma(0.5) == 1.7724538509055159
849-
assert Number.gamma(-0.5) < -3.544907 && Number.gamma(-0.5) > -3.544909
850-
assert Number.gamma(-1) == Infinity
851-
assert Number.gamma(-10) == Infinity
852-
assert Number.gamma(0.2) > 4.59084 && Number.gamma(0.2) < 4.59085
853-
assert Number.gamma(1/5) > 4.59084 && Number.gamma(1/5) < 4.59085
854-
assert Number.gamma(1/2) == 1.7724538509055159
855-
assert Number.isNaN(Number.gamma(Infinity))
856-
assert Number.isNaN(Number.gamma(NaN))
857-
858-
// Number.factorial
859-
// Note: Currently factorial will overflow the memory when using a bigint as such there are no tests for this
860-
assert Number.factorial(10) == 3628800
861-
assert Number.factorial(5) == 120
862-
assert Number.factorial(0) == 1
863-
assert Number.factorial(-5) == -120
864-
assert Number.factorial(-10) == -3628800
865-
assert Number.factorial(10.5) == 11899423.083962297
866-
assert Number.factorial(0.5) == 0.886226925452759
867-
assert Number.factorial(1/2) == 0.886226925452759
868-
assert Number.isNaN(Number.factorial(Infinity))
869-
assert Number.isNaN(Number.factorial(NaN))
870-
871811
// Number.toDegrees
872812
assert Number.toDegrees(0) == 0
873813
assert Number.toDegrees(Number.pi) == 180

stdlib/number.gr

Lines changed: 0 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -552,62 +552,6 @@ let chebyshevSine = (radians: Number) => {
552552
(radians - pi - pi_minor) * (radians + pi + pi_minor) * p1 * radians
553553
}
554554

555-
/**
556-
* Computes the sine of a number (in radians) using Chebyshev polynomials.
557-
*
558-
* @param radians: The input in radians
559-
* @returns The computed sine
560-
*
561-
* @since v0.5.2
562-
* @history v0.5.4: Handle NaN and Infinity
563-
*/
564-
provide let sin = (radians: Number) => {
565-
if (radians != radians || radians == Infinity) {
566-
NaN
567-
} else {
568-
let quot = reduceToPiBound(radians)
569-
let bounded = radians - pi * quot
570-
if (quot % 2 == 0) {
571-
chebyshevSine(bounded)
572-
} else {
573-
neg(chebyshevSine(bounded))
574-
}
575-
}
576-
}
577-
578-
/**
579-
* Computes the cosine of a number (in radians) using Chebyshev polynomials.
580-
*
581-
* @param radians: The input in radians
582-
* @returns The computed cosine
583-
*
584-
* @since v0.5.2
585-
* @history v0.5.4: Handle NaN and Infinity
586-
*/
587-
provide let cos = (radians: Number) => {
588-
if (radians != radians || radians == Infinity) {
589-
NaN
590-
} else {
591-
sin(pi / 2 + radians)
592-
}
593-
}
594-
595-
/**
596-
* Computes the tangent of a number (in radians) using Chebyshev polynomials.
597-
*
598-
* @param radians: The input in radians
599-
* @returns The computed tangent
600-
*
601-
* @since v0.5.4
602-
*/
603-
provide let tan = (radians: Number) => {
604-
if (isNaN(radians) || isInfinite(radians)) {
605-
NaN
606-
} else {
607-
sin(radians) / cos(radians)
608-
}
609-
}
610-
611555
@unsafe
612556
let rf = z => {
613557
// see: musl/src/math/asin.c and SUN COPYRIGHT NOTICE at top of file
@@ -851,83 +795,6 @@ provide let atan = angle => {
851795
return WasmI32.toGrain(newFloat64(WasmF64.copySign(z, sx))): Number
852796
}
853797

854-
// Math.gamma implemented using the Lanczos approximation
855-
// https://en.wikipedia.org/wiki/Lanczos_approximation
856-
/**
857-
* Computes the gamma function of a value using Lanczos approximation.
858-
*
859-
* @param z: The value to interpolate
860-
* @returns The gamma of the given value
861-
*
862-
* @throws InvalidArgument(String): When `z` is zero
863-
*
864-
* @since v0.5.4
865-
*/
866-
provide let rec gamma = z => {
867-
if (z == 0) {
868-
throw Exception.InvalidArgument("Gamma of 0 is undefined")
869-
} else if (isInteger(z) && z > 0) {
870-
let mut output = 1
871-
for (let mut i = 1; i < z; i += 1) {
872-
output *= i
873-
}
874-
output
875-
} else {
876-
let mut z = z
877-
let g = 7
878-
let c = [>
879-
0.99999999999980993,
880-
676.5203681218851,
881-
-1259.1392167224028,
882-
771.32342877765313,
883-
-176.61502916214059,
884-
12.507343278686905,
885-
-0.13857109526572012,
886-
9.9843695780195716e-6,
887-
1.5056327351493116e-7,
888-
]
889-
let mut output = 0
890-
if (z < 0.5) {
891-
output = pi / (sin(pi * z) * gamma(1 - z))
892-
} else if (z == 0.5) {
893-
// Handle this case separately because it is out of the domain of Number.pow when calculating
894-
output = 1.7724538509055159
895-
} else {
896-
z -= 1
897-
let mut x = c[0]
898-
for (let mut i = 1; i < g + 2; i += 1) {
899-
x += c[i] / (z + i)
900-
}
901-
902-
let t = z + g + 0.5
903-
output = sqrt(2 * pi) * (t ** (z + 0.5)) * exp(t * -1) * x
904-
}
905-
if (abs(output) == Infinity) Infinity else output
906-
}
907-
}
908-
909-
/**
910-
* Computes the product of consecutive integers for an integer input and computes the gamma function for non-integer inputs.
911-
*
912-
* @param n: The value to factorialize
913-
* @returns The factorial of the given value
914-
*
915-
* @throws InvalidArgument(String): When `n` is negative
916-
*
917-
* @since v0.5.4
918-
*/
919-
provide let rec factorial = n => {
920-
if (isInteger(n) && n < 0) {
921-
gamma(abs(n) + 1) * -1
922-
} else if (!isInteger(n) && n < 0) {
923-
throw Exception.InvalidArgument(
924-
"Cannot compute the factorial of a negative non-integer",
925-
)
926-
} else {
927-
gamma(n + 1)
928-
}
929-
}
930-
931798
/**
932799
* Converts degrees to radians.
933800
*

stdlib/number.md

Lines changed: 0 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -815,95 +815,6 @@ Returns:
815815
|----|-----------|
816816
|`Result<Number, String>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
817817

818-
### Number.**sin**
819-
820-
<details>
821-
<summary>Added in <code>0.5.2</code></summary>
822-
<table>
823-
<thead>
824-
<tr><th>version</th><th>changes</th></tr>
825-
</thead>
826-
<tbody>
827-
<tr><td><code>0.5.4</code></td><td>Handle NaN and Infinity</td></tr>
828-
</tbody>
829-
</table>
830-
</details>
831-
832-
```grain
833-
sin : (radians: Number) => Number
834-
```
835-
836-
Computes the sine of a number (in radians) using Chebyshev polynomials.
837-
838-
Parameters:
839-
840-
|param|type|description|
841-
|-----|----|-----------|
842-
|`radians`|`Number`|The input in radians|
843-
844-
Returns:
845-
846-
|type|description|
847-
|----|-----------|
848-
|`Number`|The computed sine|
849-
850-
### Number.**cos**
851-
852-
<details>
853-
<summary>Added in <code>0.5.2</code></summary>
854-
<table>
855-
<thead>
856-
<tr><th>version</th><th>changes</th></tr>
857-
</thead>
858-
<tbody>
859-
<tr><td><code>0.5.4</code></td><td>Handle NaN and Infinity</td></tr>
860-
</tbody>
861-
</table>
862-
</details>
863-
864-
```grain
865-
cos : (radians: Number) => Number
866-
```
867-
868-
Computes the cosine of a number (in radians) using Chebyshev polynomials.
869-
870-
Parameters:
871-
872-
|param|type|description|
873-
|-----|----|-----------|
874-
|`radians`|`Number`|The input in radians|
875-
876-
Returns:
877-
878-
|type|description|
879-
|----|-----------|
880-
|`Number`|The computed cosine|
881-
882-
### Number.**tan**
883-
884-
<details disabled>
885-
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
886-
No other changes yet.
887-
</details>
888-
889-
```grain
890-
tan : (radians: Number) => Number
891-
```
892-
893-
Computes the tangent of a number (in radians) using Chebyshev polynomials.
894-
895-
Parameters:
896-
897-
|param|type|description|
898-
|-----|----|-----------|
899-
|`radians`|`Number`|The input in radians|
900-
901-
Returns:
902-
903-
|type|description|
904-
|----|-----------|
905-
|`Number`|The computed tangent|
906-
907818
### Number.**asin**
908819

909820
<details disabled>
@@ -979,68 +890,6 @@ Returns:
979890
|----|-----------|
980891
|`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`|
981892

982-
### Number.**gamma**
983-
984-
<details disabled>
985-
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
986-
No other changes yet.
987-
</details>
988-
989-
```grain
990-
gamma : (z: Number) => Number
991-
```
992-
993-
Computes the gamma function of a value using Lanczos approximation.
994-
995-
Parameters:
996-
997-
|param|type|description|
998-
|-----|----|-----------|
999-
|`z`|`Number`|The value to interpolate|
1000-
1001-
Returns:
1002-
1003-
|type|description|
1004-
|----|-----------|
1005-
|`Number`|The gamma of the given value|
1006-
1007-
Throws:
1008-
1009-
`InvalidArgument(String)`
1010-
1011-
* When `z` is zero
1012-
1013-
### Number.**factorial**
1014-
1015-
<details disabled>
1016-
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
1017-
No other changes yet.
1018-
</details>
1019-
1020-
```grain
1021-
factorial : (n: Number) => Number
1022-
```
1023-
1024-
Computes the product of consecutive integers for an integer input and computes the gamma function for non-integer inputs.
1025-
1026-
Parameters:
1027-
1028-
|param|type|description|
1029-
|-----|----|-----------|
1030-
|`n`|`Number`|The value to factorialize|
1031-
1032-
Returns:
1033-
1034-
|type|description|
1035-
|----|-----------|
1036-
|`Number`|The factorial of the given value|
1037-
1038-
Throws:
1039-
1040-
`InvalidArgument(String)`
1041-
1042-
* When `n` is negative
1043-
1044893
### Number.**toRadians**
1045894

1046895
<details disabled>

0 commit comments

Comments
 (0)