Mathematical Functions
The math module bundles various mathematical and trigonometrical functions.
Functions can be individually imported and directly accessed using the named import syntax:
import { pow, rand } from 'math';
let x = pow(2, 5);
let y = rand();
Alternatively, the module namespace can be imported using a wildcard import statement:
import * as math from 'math';
let x = math.pow(2, 5);
let y = math.rand();
Additionally, the math module namespace may also be imported by invoking the ucode interpreter with the -lmath switch.
- Source
Methods
abs(number) → {number}
Returns the absolute value of the given numeric value.
| Name | Type | Description |
|---|---|---|
number | * | The number to return the absolute value for. |
Returns the absolute value or NaN if the given argument could not be converted to a number.
- Source
atan2(y, x) → {number}
Calculates the principal value of the arc tangent of y/x, using the signs of the two arguments to determine the quadrant of the result.
On success, this function returns the principal value of the arc tangent of y/x in radians; the return value is in the range [-pi, pi].
- If
yis +0 (-0) andxis less than 0, +pi (-pi) is returned. - If
yis +0 (-0) andxis greater than 0, +0 (-0) is returned. - If
yis less than 0 andxis +0 or -0, -pi/2 is returned. - If
yis greater than 0 andxis +0 or -0, pi/2 is returned. - If either
xoryis NaN, a NaN is returned. - If
yis +0 (-0) andxis -0, +pi (-pi) is returned. - If
yis +0 (-0) andxis +0, +0 (-0) is returned. - If
yis a finite value greater (less) than 0, andxis negative infinity, +pi (-pi) is returned. - If
yis a finite value greater (less) than 0, andxis positive infinity, +0 (-0) is returned. - If
yis positive infinity (negative infinity), andxis finite, pi/2 (-pi/2) is returned. - If
yis positive infinity (negative infinity) andxis negative infinity, +3pi/4 (-3pi/4) is returned. - If
yis positive infinity (negative infinity) andxis positive infinity, +pi/4 (-pi/4) is returned.
When either x or y can't be converted to a numeric value, NaN is returned.
| Name | Type | Description |
|---|---|---|
y | * | The |
x | * | The |
- Source
cos(x) → {number}
Calculates the cosine of x, where x is given in radians.
Returns the resulting consine value.
Returns NaN if the x value can't be converted to a number.
| Name | Type | Description |
|---|---|---|
x | number | Radians value to calculate cosine for. |
- Source
exp(x) → {number}
Calculates the value of e (the base of natural logarithms) raised to the power of x.
On success, returns the exponential value of x.
- If
xis positive infinity, positive infinity is returned. - If
xis negative infinity,+0is returned. - If the result underflows, a range error occurs, and zero is returned.
- If the result overflows, a range error occurs, and
Infinityis returned.
Returns NaN if the x value can't be converted to a number.
| Name | Type | Description |
|---|---|---|
x | number | Power to raise |
- Source
isnan(x) → {boolean}
Tests whether x is a NaN double.
This functions checks whether the given argument is of type double with a NaN (not a number) value.
Returns true if the value is NaN, otherwise false.
Note that a value can also be checked for NaN with the expression x !== x which only evaluates to true if x is NaN.
| Name | Type | Description |
|---|---|---|
x | number | The value to test. |
- Source
log(x) → {number}
Calculates the natural logarithm of x.
On success, returns the natural logarithm of x.
- If
xis1, the result is+0. - If
xis positive nfinity, positive infinity is returned. - If
xis zero, then a pole error occurs, and the function returns negative infinity. - If
xis negative (including negative infinity), then a domain error occurs, andNaNis returned.
Returns NaN if the x value can't be converted to a number.
| Name | Type | Description |
|---|---|---|
x | number | Value to calulate natural logarithm of. |
- Source
pow(x, y) → {number}
Calculates the value of x raised to the power of y.
On success, returns the value of x raised to the power of y.
- If the result overflows, a range error occurs, and the function returns
Infinity. - If result underflows, and is not representable, a range error occurs, and
0.0with the appropriate sign is returned. - If
xis+0or-0, andyis an odd integer less than0, a pole error occursInfinityis returned, with the same sign asx. - If
xis+0or-0, andyis less than0and not an odd integer, a pole error occurs andInfinityis returned. - If
xis+0(-0), andyis an odd integer greater than0, the result is+0(-0). - If
xis0, andygreater than0and not an odd integer, the result is+0. - If
xis-1, andyis positive infinity or negative infinity, the result is1.0. - If
xis+1, the result is1.0(even ifyisNaN). - If
yis0, the result is1.0(even ifxisNaN). - If
xis a finite value less than0, andyis a finite noninteger, a domain error occurs, andNaNis returned. - If the absolute value of
xis less than1, andyis negative infinity, the result is positive infinity. - If the absolute value of
xis greater than1, andyis negative infinity, the result is+0. - If the absolute value of
xis less than1, andyis positive infinity, the result is+0. - If the absolute value of
xis greater than1, andyis positive infinity, the result is positive infinity. - If
xis negative infinity, andyis an odd integer less than0, the result is-0. - If
xis negative infinity, andyless than0and not an odd integer, the result is+0. - If
xis negative infinity, andyis an odd integer greater than0, the result is negative infinity. - If
xis negative infinity, andygreater than0and not an odd integer, the result is positive infinity. - If
xis positive infinity, andyless than0, the result is+0. - If
xis positive infinity, andygreater than0, the result is positive infinity.
Returns NaN if either the x or y value can't be converted to a number.
| Name | Type | Description |
|---|---|---|
x | number | The base value. |
y | number | The power value. |
- Source
rand(aopt, bopt) → {number}
Depending on the arguments, it produces a pseudo-random positive integer, or a pseudo-random number in a supplied range.
Without arguments it returns the calculated pseuo-random value. The value is within the range 0 to RAND_MAX inclusive where RAND_MAX is a platform specific value guaranteed to be at least 32767.
With 2 arguments a, b it returns a number in the range a to b inclusive. With a single argument a it returns a number in the range 0 to a inclusive.
The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.
If no seed value is explicitly set by calling srand() prior to the first call to rand(), the math module will automatically seed the PRNG once, using the current time of day in milliseconds as seed value.
| Name | Type | Description |
|---|---|---|
a | number | (optional) End of the desired range. |
b | number | (optional) The other end of the desired range. |
- Source
sin(x) → {number}
Calculates the sine of x, where x is given in radians.
Returns the resulting sine value.
- When
xis positive or negative infinity, a domain error occurs andNaNis returned.
Returns NaN if the x value can't be converted to a number.
| Name | Type | Description |
|---|---|---|
x | number | Radians value to calculate sine for. |
- Source
sqrt(x) → {number}
Calculates the nonnegative square root of x.
Returns the resulting square root value.
- If
xis+0(-0) then+0(-0) is returned. - If
xis positive infinity, positive infinity is returned. - If
xis less than-0, a domain error occurs, andNaNis returned.
Returns NaN if the x value can't be converted to a number.
| Name | Type | Description |
|---|---|---|
x | number | Value to calculate square root for. |
- Source
srand(seed)
Seeds the pseudo-random number generator.
This functions seeds the PRNG with the given value and thus affects the pseudo-random integer sequence produced by subsequent calls to rand().
Setting the same seed value will result in the same pseudo-random numbers produced by rand().
| Name | Type | Description |
|---|---|---|
seed | number | The seed value. |
- Source