Skip to content

Commit

Permalink
Add missing Math constants and functions (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
farism authored Aug 27, 2023
1 parent bcab4a2 commit 0668cac
Show file tree
Hide file tree
Showing 3 changed files with 373 additions and 0 deletions.
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
193 changes: 193 additions & 0 deletions core/source/Math.mint
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
/* Mathematical functions. */
module Math {
/* Euler's number and the base of natural logarithms; approximately `2.718`. */
const E = `Math.E`

/* Natural logarithm of `10`; approximately `2.303`. */
const LN10 = `Math.LN10`

/* Natural logarithm of `2`; approximately `0.693`. */
const LN2 = `Math.LN2`

/* Base-10 logarithm of `E`; approximately `0.434`. */
const LOG10E = `Math.LOG10E`

/* Base-2 logarithm of `E`; approximately `1.443`. */
const LOG2E = `Math.LOG2E`

/* Ratio of a circle's circumference to its diameter; approximately `3.14159`. */
const PI = `Math.PI`

/* Square root of `0.5`; approximately `0.707`. */
const SQRT12 = `Math.SQRT1_2`

/* Square root of `2`; approximately `1.414`. */
const SQRT2 = `Math.SQRT2`

/*
Returns the absolute value of the given number.
Expand All @@ -10,6 +34,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 +98,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 +152,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 +261,26 @@ 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 */
fun sin (value : Number) : Number {
`Math.sin(#{value})`
}

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

/*
Returns the square root of the given number
Expand All @@ -107,6 +290,16 @@ module Math {
`Math.sqrt(#{value})`
}

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

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

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

0 comments on commit 0668cac

Please sign in to comment.