# Math
Mathematical operations as known from JavaScript.
The Math API is very much like JavaScript's (MDN (opens new window)), with the notable exceptions stated above and rest parameters not being supported yet.
# Variants
Math in AssemblyScript is available in multiple variants.
| Variant | Description |
|---|---|
| NativeMath | WebAssembly implementation for f64 |
| NativeMathf | WebAssembly implementation for f32 |
| JSMath | JavaScript implementation for f64 (imported from the host) |
By default, the global Math object is an alias of NativeMath and Mathf is an alias of NativeMathf .
# Using JSMath
By default, the compiler utilizes NativeMath which is implemented in WebAssembly directly, but where small module size is more important than performance, one can opt to override the default by adding --use Math=JSMath on the command line, essentially aliasing Math with JSMath instead, which maps to an import of the browser's math implementation. Naturally, this option requires importing the browser's Math object as a whole, but does not require seeding / is not seedable. Generated bindings automatically take care of importing the browser's math in this scenario.
# Static members
The type T below substitutes either f32 or f64 depending on the implementation used. Note that Math is not actually generic, but concrete implementations like Math and Mathf have an identical interface that only differs in the implementation's floating-point precision denoted by T.
# Constants
const E: TThe base of natural logarithms, e, approximately 2.718.
const LN2: TThe natural logarithm of 2, approximately 0.693.
const LN10: TThe natural logarithm of 10, approximately 2.302.
const LOG2E: TThe base 2 logarithm of e, approximately 1.442.
const LOG10E: TThe base 10 logarithm of e, approximately 0.434.
const PI: TThe ratio of the circumference of a circle to its diameter, approximately 3.14159.
const SQRT1_2: TThe square root of 1/2, approximately 0.707.
const SQRT2: TThe square root of 2, approximately 1.414.
# Functions
function abs(x: T): TReturns the absolute value of
x.function acos(x: T): TReturns the arccosine (in radians) of
x.function acosh(x: T): TReturns the hyperbolic arc-cosine of
x.function asin(x: T): TReturns the arcsine (in radians) of
x.function asinh(x: T): TReturns the hyperbolic arcsine of
x.function atan(x: T): TReturns the arctangent (in radians) of
x.function atan2(y: T, x: T): TReturns the arctangent of the quotient of its arguments.
function atanh(x: T): TReturns the hyperbolic arctangent of
x.function cbrt(x: T): TReturns the cube root of
x.function ceil(x: T): TReturns the smallest integer greater than or equal to
x.function clz32(x: T): TReturns the number of leading zero bits in the 32-bit binary representation of
x.function cos(x: T): TReturns the cosine (in radians) of
x.function cosh(x: T): TReturns the hyperbolic cosine of
x.function exp(x: T): TReturns e to the power of
x.function expm1(x: T): TReturns e to the power of
x, minus 1.function floor(x: T): TReturns the largest integer less than or equal to
x.function fround(x: T): TReturns the nearest 32-bit single precision float representation of
x.function hypot(value1: T, value2: T): TReturns the square root of the sum of squares of its arguments.
function imul(a: T, b: T): TReturns the result of the C-like 32-bit multiplication of
aandb.function log(x: T): TReturns the natural logarithm (base e) of
x.function log10(x: T): TReturns the base 10 logarithm of
x.function log1p(x: T): TReturns the natural logarithm (base e) of 1 +
x.function log2(x: T): TReturns the base 2 logarithm of
x.function max(value1: T, value2: T): TReturns the largest-valued number of its arguments.
function min(value1: T, value2: T): TReturns the lowest-valued number of its arguments.
function pow(base: T, exponent: T): TReturns
baseto the power ofexponent.function random(): TReturns a pseudo-random number in the range from 0.0 inclusive up to but not including 1.0.
Note that
Math.randomneeds a way to seed the random number generator, which is achieved with a special importenv.seedreturning the seed as anf64value. Generated bindings and WASI take care of it automatically.function round(x: T): TReturns the value of
xrounded to the nearest integer.function sign(x: T): TReturns the sign of
x, indicating whether the number is positive, negative or zero.function signbit(x: T): boolReturns whether the sign bit of
xis set.function sin(x: T): TReturns the sine of
x.function sinh(x: T): TReturns the hyperbolic sine of
x.function sqrt(x: T): TReturns the square root of
x.function tan(x: T): TReturns the tangent of
x.function tanh(x: T): TReturns the hyperbolic tangent of
x.function trunc(x: T): TReturns the integer part of
xby removing any fractional digits.
# Considerations
The Math implementations are meant as a drop-in replacement for JavaScript's Math with all its quirks. For example, Math.round always rounds towards +Infinity and Math.imul and similar functions operate on number, which is f64 in AssemblyScript. Hence, using WebAssembly's math instructions directly where possible is often more efficient.