-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
feat(stdlib): Add Number.tan
, Number.factorial
, Number.clamp
, Number.lerp
#1373
Changes from all commits
d00b9f7
c557d53
207018f
1d9a8eb
8502063
47436cd
e3b72d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -27,21 +27,21 @@ import Tags from "runtime/unsafe/tags" | |||||
|
||||||
/** | ||||||
* Pi represented as a Number value. | ||||||
* | ||||||
* | ||||||
* @since v0.5.2 | ||||||
*/ | ||||||
export let pi = 3.141592653589793 | ||||||
|
||||||
/** | ||||||
* Tau represented as a Number value. | ||||||
* | ||||||
* | ||||||
* @since v0.5.2 | ||||||
*/ | ||||||
export let tau = 6.283185307179586 | ||||||
|
||||||
/** | ||||||
* Euler's number represented as a Number value. | ||||||
* | ||||||
* | ||||||
* @since v0.5.2 | ||||||
*/ | ||||||
export let e = 2.718281828459045 | ||||||
|
@@ -439,3 +439,63 @@ export let sin = (radians: Number) => { | |||||
export let cos = (radians: Number) => { | ||||||
sin(pi / 2 + radians) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Computes the tangent of a number (in radians) using Chebyshev polynomials. | ||||||
* | ||||||
* @param radians: The input in radians | ||||||
* @returns The computed tangent | ||||||
* | ||||||
* @since v0.5.3 | ||||||
*/ | ||||||
export let tan = (radians: Number) => { | ||||||
sin(radians) / cos(radians) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Computes the factorial of a number. | ||||||
* | ||||||
* @param input: The number to compute the factorial of, Must be a Non negative integer | ||||||
* @returns The computed factorial | ||||||
* | ||||||
* @since v0.5.3 | ||||||
*/ | ||||||
export let factorial = input => { | ||||||
spotandjake marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// Error If It is Negative, or Not an Integer | ||||||
if (sign(input) is -1 || isInteger(input)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
this is better, right? |
||||||
fail "Factorial Expects A Non Negative Integer Input" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we actually want to fail or return an option? @ospencer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should fail. If we really don't want it to fail then we can take Jake's suggestion to define this as the gamma function for non-integers. |
||||||
} else { | ||||||
// Factorial Helper | ||||||
let rec fact = n => if (n is 0) 1 else n * fact(n - 1) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
why use |
||||||
// Perform Factorial | ||||||
fact(input) | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Clamps a number to a given range. | ||||||
* | ||||||
* @param min: The minimum value in the range | ||||||
* @param max: The maximum value in the range | ||||||
Comment on lines
+478
to
+479
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did someone say Range? |
||||||
* @param input: The number to clamp | ||||||
* @returns The clamped number | ||||||
* | ||||||
* @since v0.5.3 | ||||||
*/ | ||||||
export let clamp = (min: Number, max: Number, input: Number) => { | ||||||
if (input < min) min else if (input > max) max else input | ||||||
} | ||||||
|
||||||
/** | ||||||
* Linearly interpolates between the starting and ending value by the given amount. | ||||||
* | ||||||
* @param start: The starting value | ||||||
* @param end: The ending value | ||||||
* @param amount: an amount between 0 and 1, anything outside the range will produce an extrapolation i.e `lerp(0, 100, 2)` will produce 200 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a "interpolation" but you are saying that it also "exptrapolates". I don't like that. |
||||||
* @returns The interpolated value | ||||||
* | ||||||
* @since v0.5.3 | ||||||
*/ | ||||||
export let lerp = (start: Number, end: Number, amount: Number) => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Hear me out. |
||||||
(end - start) * amount + start | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.