Math
Standard constants and functions related to mathematics and numerical computation are assigned the namespace of Math:
.
Constants
The type is all num
.
Name | Description | Rough Value |
---|---|---|
Math:Infinity | Positive infinity | N/A |
Math:E | Napier number | 2.718281828459045 |
Math:LN2 | Natural logarithm of 2 | 0.6931471805599453 |
Math:LN10 | Natural logarithm of 10 | 2.302585092994046 |
Math:LOG2E | Logarithm of | 1.4426950408889634 |
Math:LOG10E | Logarithm of | 0.4342944819032518 |
Math:PI | Pi | 3.141592653589793 |
Math:SQRT1_2 | 0.7071067811865476 | |
Math:SQRT2 | 1.4142135623730951 |
Basic Functions
@Math:abs(x: num): num
Calculate absolute value.
@Math:sign(x: num): num
Returns 1 if
Returns NaN if
@Math:round(x: num): num
Returns the nearest integer, rounded to the nearest whole number.
@Math:ceil(x: num): num
Returns the smallest integer greater than or equal to the argument.
@Math:floor(x: num): num
Returns the largest integer less than or equal to the argument.
@Math:trunc(x: num): num
Truncates the decimal portion of the argument and returns the integer portion.
@Math:min(a: num, b: num): num
Returns the smaller value.
@Math:max(a: num, b: num): num
Returns the larger value.
@Math:sqrt(x: num): num
Computes the positive square root.
@Math:cbrt(x: num): num
Calculate the cubic root.
@Math:hypot(vs: arr): num
Returns the positive square root of the sum of the elements of vs after squaring each of them.
Trigonometric Functions
Hereafter, all angles are in radians.
@Math:sin(rad: num): num
Returns sine.
@Math:cos(rad: num): num
Returns cosine.
@Math:tan(rad: num): num
Returns tangent.
@Math:asin(x: num): num
Returns arcsine.
@Math:acos(x: num): num
Returns arccosine.
@Math:atan(x: num): num
Returns arctangent.
@Math:atan2(y: num, x: num): num
Returns the tangent of
Hyperbolic Functions
@Math:sinh(x: num): num
Returns hyperbolic sine.
@Math:cosh(x: num): num
Returns hyperbolic cosine.
@Math:tanh(x: num): num
Returns hyperbolic tangent.
@Math:asinh(x: num): num
Returns hyperbolic arcsine.
@Math:acosh(x: num): num
Returns hyperbolic arccosine.
@Math:atanh(x: num): num
Returns hyperbolic arctangent.
Exponential / Logarithmic Functions
@Math:pow(x: num, y: num): num
Returns the y power of x. Almost identical to Core:pow
except that it internally uses the Math.pow
function instead of Javascript's **
operator.
@Math:exp(x: num): num
Returns the x power of
@Math:expm1(x: num): num
Returns the x power of
@Math:log(x: num): num
Returns the natural logarithm. Use Math:log10
for the ordinary logarithm.
@Math:log1p(x: num): num
Returns the natural logarithm of x + 1.
@Math:log10(x: num): num
Returns the logarithm with 10 as the base (ordinary logarithm).
@Math:log2(x: num): num
Returns the logarithm with 2 as the base.
Random Generators
@Math:rnd(min?: num, max?: num): num
Generates random numbers.
If min and max are passed, integers with min <= x and x <= max are returned, otherwise decimals with 0 <= x and x < 1 are returned.
@Math:gen_rng(seed: num | str, options?: obj): @(min?: num, max?: num)
Generates a random number generator from the seed.
The generated random number generator returns an integer with min <= x, x <= max if min and max are passed,
If not passed, it returns a floating point number with 0 <= x, x < 1.
You can specify the internal behavior through the object passed to options.
The change in behavior by specifying options.algorithm
is shown below:
options.algorithm | Internal randomize algorhythm | Range-specified integer generation algorithm |
---|---|---|
rc4 | RC4 | Rejection Sampling |
rc4_legacy | RC4 | Range limitation by floating-point arithmetic (pre-0.19.0 algorithm) |
Not specified, or chacha20 | ChaCha20 | Rejection Sampling |
let rng = Math:gen_rng('AICHAN')
// Seed value is fixed, so under the same conditions,
// the same generation pattern will be generated each time.
<: rng(0, 10)
<: rng(0, 10)
<: rng(0, 10)
WARNING
Range-specified integer generation algorithms with floating-point arithmetic, such as rc4_legacy
, may generate values larger than the specified max value due to rounding errors during arithmetic.
Misc.
@Math:clz32(x: num): num
Returns the number of leading zeros when x is represented in 32-bit binary.
@Math:fround(x: num): num
Returns the value when x is converted to a 32-bit floating-point number.
@Math:imul(x: num, y: num): num
Returns the result of a C-like 32-bit multiplication of x and y.