mathplugin
sin, cos, pow and the rest with the same bits on every JavaScript engine.
Using it
Math has a server half only. It has no row, no world.math and no entry in plugins. A rule, a plugin's server half or a mod imports it and calls math.sin( a ) where it would call Math.sin( a ), and math.pow( a, b ) where it would write a ** b:
import * as math from '#plugins/math/math.sim.ts'
JavaScript leaves Math.sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh, asinh, acosh, atanh, exp, expm1, log, log1p, log2, log10, pow (and **), cbrt and hypot to each engine, and engines differ in the last bit: node 22 and Chrome 157 differed on 2 to 16 inputs in every 100 for each of them but hypot. A page that runs a scene ahead steps the same rules as the server, so a rule that called one of them would end a hair from the server's truth. The functions here return the same double for the same arguments on node, in every browser and in every version of either.
Each is within 1 ulp of the true value, and takes and returns what its Math namesake does for NaN, ±0 and ±Infinity. Angles are in radians and may be of any size.
What Math computes exactly stays on Math: sqrt, abs, floor, ceil, round, trunc, sign, min, max, fround, imul, clz32, PI, E and the other constants. Code that only draws, such as a page's browser half or an extension, may use Math throughout.
The three.js this project builds with calls these functions for every angle in its own maths, so a rule may use three's vectors, quaternions and matrices.
Trigonometry
| Function | Returns |
|---|---|
sin( x ) | The sine of x radians. NaN for ±Infinity. |
cos( x ) | The cosine of x radians. NaN for ±Infinity. |
tan( x ) | The tangent of x radians. NaN for ±Infinity. |
asin( x ) | The angle in radians, −π/2 to π/2, whose sine is x. NaN when x is outside −1 to 1. |
acos( x ) | The angle in radians, 0 to π, whose cosine is x. NaN when x is outside −1 to 1. |
atan( x ) | The angle in radians, −π/2 to π/2, whose tangent is x. |
atan2( y, x ) | The angle in radians, −π to π, of the point (x, y) counter-clockwise from the positive x axis. Note the order: y first. |
Hyperbolic
| Function | Returns |
|---|---|
sinh( x ), cosh( x ), tanh( x ) | The hyperbolic sine, cosine and tangent of x. |
asinh( x ) | The inverse hyperbolic sine of x. |
acosh( x ) | The inverse hyperbolic cosine of x. NaN when x is below 1. |
atanh( x ) | The inverse hyperbolic tangent of x. ±Infinity at ±1, and NaN outside −1 to 1. |
Exponentials and logarithms
| Function | Returns |
|---|---|
exp( x ) | e to the power x. Infinity above about 709.78, and 0 below about −745.13. |
expm1( x ) | e to the power x, minus 1, accurate for x near 0. |
log( x ) | The natural logarithm of x. −Infinity at 0 and NaN below 0. |
log1p( x ) | The natural logarithm of 1 + x, accurate for x near 0. −Infinity at −1 and NaN below it. |
log2( x ) | The base 2 logarithm of x. −Infinity at 0 and NaN below 0. |
log10( x ) | The base 10 logarithm of x. −Infinity at 0 and NaN below 0. |
Powers and roots
| Function | Returns |
|---|---|
pow( x, y ) | x to the power y, as Math.pow and ** give it. 1 when y is 0. |
cbrt( x ) | The cube root of x. A negative x gives a negative root. |
hypot( ...xs ) | The square root of the sum of the squares of its arguments, without overflow or underflow on the way. Any ±Infinity gives Infinity, then any NaN gives NaN, and no arguments or all zeros give 0. |
Used by
Every module whose rules compute with angles, exponents or powers, among them mapgen, nav, ocean3d and scatter3d.