Skip to content
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

Add missing Math constants and functions #631

Merged
merged 2 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/lib/
/bin/
/.shards/
.vscode
194 changes: 194 additions & 0 deletions core/source/Math.mint
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
/* Mathematical functions. */
module Math {
const E = `Math.E`
const LN10 = `Math.LN10`
const LN2 = `Math.LN2`
const LOG10E = `Math.LOG10E`
const LOG2E = `Math.LOG2E`
const PI = `Math.PI`
const SQRT12 = `Math.SQRT1_2`
const SQRT2 = `Math.SQRT2`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if these constants have comments as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 👍


/*
Returns the absolute value of the given number.

Expand All @@ -10,6 +19,51 @@ module Math {
`Math.abs(#{number})`
}

/* Returns the inverse cosine of the given angle in radians */
fun acos (number : Number) : Number {
`Math.acos(#{number})`
}

/* Returns the inverse hyperbolic cosine of the given angle in radians */
fun acosh (number : Number) : Number {
`Math.acosh(#{number})`
}

/* Returns the inverse sine of the given angle in radians */
fun asin (number : Number) : Number {
`Math.asin(#{number})`
}

/* Returns the inverse hyperbolic sine of the given angle in radians */
fun asinh (number : Number) : Number {
`Math.asinh(#{number})`
}

/* Returns the inverse tangent of the given angle in radians */
fun atan (number : Number) : Number {
`Math.atan(#{number})`
}

/* Returns the angle in the plane (in radians) between the positive x-axis and the ray from (0, 0) to the point (x, y) */
fun atan2 (y : Number, x : Number) : Number {
`Math.atan2(#{y}, #{x})`
}

/* Returns the inverse hyperbolic tangent of the given angle in radians */
fun atanh (number : Number) : Number {
`Math.atanh(#{number})`
}

/*
Returns the cubic root of the given number

Math.cbrt(1) == 1
Math.cbrt(64) == 4
*/
fun cbrt (number : Number) : Number {
`Math.cbrt(#{number})`
}

/*
Returns the smallest integer greater than or equal to the given number.

Expand All @@ -29,6 +83,41 @@ module Math {
Math.min(upper, Math.max(lower, value))
}

/*
Returns the number of leading zero bits in the 32-bit binary representation of a number

00000000000000000000000000000100
Math.clz32(4) == 29
*/
fun clz32 (number : Number) : Number {
`Math.clz32(#{number})`
}

/* Returns the cosine of the given angle in radians */
fun cos (number : Number) : Number {
`Math.cos(#{number})`
}

/* Returns the hyperbolic cosine of the given angle in radians */
fun cosh (number : Number) : Number {
`Math.cosh(#{number})`
}

/* Returns the value of `Math:E` raised to the power x, where x is the given number */
fun exp (x : Number) : Number {
`Math.exp(#{x})`
}

/*
Returns the value of `Math:E` to the power x, minus 1

Math.exp(2) == 7.38905609893065
Math.expm1(2) == 6.38905609893065
*/
fun expm1 (x : Number) : Number {
`Math.expm1(#{x})`
}

/*
Returns the largest integer less than or equal to the given number.

Expand All @@ -48,6 +137,65 @@ module Math {
`Number((#{b} - (Math.floor(#{b} / #{a}) * #{a})).toPrecision(8))`
}

/* Returns the nearest 32-bit single precision float representation of the given number. */
fun fround (number : Number) : Number {
`Math.fround(#{number})`
}

/*
Returns the square root of the sum of squares of its arguments.

Math.hypot(3, 4) == 5
*/
fun hypot (a : Number, b : Number) : Number {
`Math.hypot(#{a}, #{b})`
}

/*
Returns the result using C-like 32-bit multiplication of the two parameters.

Math.imul(3, 4) == 12
*/
fun imul (a : Number, b : number) : Number {
`Math.imul(#{a}, #{b})`
}

/*
Returns natural logarithm (base e) of the given value

Math.log(1) == 0
*/
fun log (number : Number) : Number {
`Math.log(#{number})`
}

/*
Returns natural logarithm (base 10) of the given value

Math.log10(100) == 10
*/
fun log10 (number : Number) : Number {
`Math.log10(#{number})`
}

/*
Returns natural logarithm (base e) of the given value, plus 1

Math.log1p(1) == 0
*/
fun log1p (number : Number) : Number {
`Math.log1p(#{number})`
}

/*
Returns natural logarithm (base 2) of the given value

Math.log2(8) == 3
*/
fun log2 (number : Number) : Number {
`Math.log2(#{number})`
}

/*
Returns the highest-valued number from the arguments.

Expand Down Expand Up @@ -98,6 +246,34 @@ module Math {
`Math.round(#{number})`
}

/*
Returns the sign of the given number (1 or -1)

Math.sign(5) == 1
Math.sign(-5) == -1
*/
fun sign (number : Number) : Number {
`Math.sign(#{number})`
}

/*
Calculates the sine of the given angle in radians

Math.sin(Math.PI / 2) == 1
*/
fun sin (value : Number) : Number {
`Math.sin(#{value})`
}

/*
Calculates the hyperbolic sine of the given angle in radians

Math.sinh(Math.PI / 2) == 1
*/
fun sinh (number : Number) : Number {
`Math.sinh(#{number})`
}

/*
Returns the square root of the given number

Expand All @@ -107,6 +283,24 @@ module Math {
`Math.sqrt(#{value})`
}

/*
Calculates the tangent of the given angle in radians

Math.tan(Math.PI) == 13
*/
fun tan (number : Number) {
`Math.tan(#{number})`
}

/*
Calculates the hyperbolic tangent of the given angle in radians

Math.tanh(Math.PI) == 13
*/
fun tanh (number : Number) : Number {
`Math.tanh(#{number})`
}

/*
Returns the integer part of a number by removing any fractional digits.

Expand Down
Loading