Math

Standard JavaScript Math library plus embedded extensions: Math.map() scaling and a hardware random number generator.

The standard JavaScript Math library is available, with two embedded specialties documented below: Math.map() for scaling sensor values and Math.random() backed by the hardware random number generator.

Constants: Math.E (≈2.718), Math.LN10 (≈2.303), Math.LN2 (≈0.693), Math.LOG10E (≈0.434), Math.LOG2E (≈1.443), Math.PI (≈3.14159), Math.SQRT1_2 (≈0.707), Math.SQRT2 (≈1.414).

Standard methods: abs, acos, acosh, asin, asinh, atan, atan2, atanh, cbrt, ceil, cos, cosh, exp, floor, log, max, min, pow, round, sin, sinh, sqrt, tan, tanh, trunc — all behave as in standard JavaScript.

Functions

Math.random()

Math.random() → number

Returns a 32-bit random number from the hardware random number generator.

Unlike standard JavaScript, Math.random() on the Q5C is backed by the hardware random number generator, making it suitable for nonces and keys.

Returns

number 32-bit hardware random number.

Example

let nonce = Math.random();

Math.abs()

Math.abs(x) → number

Returns the absolute value of x.

Returns x without its sign. Negative values become positive; positive values and zero are returned unchanged.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number The absolute value of x.

Example

let d = Math.abs(-5); // 5

Math.acos()

Math.acos(x) → number

Returns the arccosine of x.

Returns the angle, in radians, whose cosine is x. The result is in the range 0 to π. Returns NaN if x is outside -1 to 1.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number between -1 and 1.

Returns

number The arccosine of x, in radians.

Example

let a = Math.acos(0.5); // 1.0471975511965979

Math.acosh()

Math.acosh(x) → number

Returns the hyperbolic arccosine of x.

Returns the hyperbolic arccosine of x. Returns NaN if x is less than 1.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number greater than or equal to 1.

Returns

number The hyperbolic arccosine of x.

Example

let a = Math.acosh(2); // 1.3169578969248166

Math.asin()

Math.asin(x) → number

Returns the arcsine of x.

Returns the angle, in radians, whose sine is x. The result is in the range -π/2 to π/2. Returns NaN if x is outside -1 to 1.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number between -1 and 1.

Returns

number The arcsine of x, in radians.

Example

let a = Math.asin(1); // 1.5707963267948966

Math.asinh()

Math.asinh(x) → number

Returns the hyperbolic arcsine of a number.

Returns the hyperbolic arcsine of x.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number The hyperbolic arcsine of x.

Example

let a = Math.asinh(1); // 0.881373587019543

Math.atan()

Math.atan(x) → number

Returns the arctangent of x.

Returns the angle, in radians, whose tangent is x. The result is in the range -π/2 to π/2.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number The arctangent of x, in radians.

Example

let a = Math.atan(1); // 0.7853981633974483

Math.atan2()

Math.atan2(y, x) → number

Returns the arctangent of the quotient of its arguments.

Returns the angle, in radians, between the positive x-axis and the point (x, y). Note the argument order: y comes first. The result is in the range -π to π, and the signs of both arguments are used to determine the quadrant.

Arguments

NameTypeRequiredDefaultDescription
y number optional — The y coordinate.
x number optional — The x coordinate.

Returns

number The angle in radians between the positive x-axis and the point (x, y).

Example

let heading = Math.atan2(1, 1); // 0.7853981633974483

Math.atanh()

Math.atanh(x) → number

Returns the hyperbolic arctangent of x.

Returns the hyperbolic arctangent of x. Returns Infinity or -Infinity for 1 and -1, and NaN if x is outside that range.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number between -1 and 1.

Returns

number The hyperbolic arctangent of x.

Example

let a = Math.atanh(0.5); // 0.5493061443340548

Math.cbrt()

Math.cbrt(x) → number

Returns the cube root of x.

Returns the cube root of x. Unlike Math.sqrt(), negative values are allowed and return a negative result.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number The cube root of x.

Example

let c = Math.cbrt(27); // 3

Math.ceil()

Math.ceil(x) → number

Returns the smallest integer greater than or equal to x.

Rounds x up to the next integer. Integers are returned unchanged.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number The smallest integer greater than or equal to x.

Example

let c = Math.ceil(4.2); // 5

Math.cos()

Math.cos(x) → number

Returns the cosine of x.

Returns the cosine of the angle x, given in radians. The result is in the range -1 to 1.

Arguments

NameTypeRequiredDefaultDescription
x number optional — An angle in radians.

Returns

number The cosine of x.

Example

let c = Math.cos(Math.PI); // -1

Math.cosh()

Math.cosh(x) → number

Returns the hyperbolic cosine of x.

Returns the hyperbolic cosine of x.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number The hyperbolic cosine of x.

Example

let c = Math.cosh(1); // 1.5430806348152437

Math.exp()

Math.exp(x) → number

Returns e^x, where e is Euler's number.

Returns e raised to the power of x, where e is Euler's number (approximately 2.718), the base of the natural logarithm.

Arguments

NameTypeRequiredDefaultDescription
x number optional — The exponent.

Returns

number e raised to the power x.

Example

let e = Math.exp(1); // 2.718281828459045

Math.floor()

Math.floor(x) → number

Returns the largest integer less than or equal to x.

Rounds x down to the previous integer. Integers are returned unchanged. For negative values this rounds away from zero; use Math.trunc() to round toward zero.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number The largest integer less than or equal to x.

Example

let f = Math.floor(4.8); // 4

Math.log()

Math.log(x) → number

Returns the natural logarithm (base e) of x.

Returns the natural logarithm (㏑) of x. Returns -Infinity for 0 and NaN for negative values.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number greater than or equal to 0.

Returns

number The natural logarithm of x.

Example

let l = Math.log(Math.E); // 1

Math.max()

Math.max(...values) → number

Returns the largest of zero or more numbers.

Returns the largest of the given numbers. Returns -Infinity if no arguments are given, and NaN if any argument is NaN.

Arguments

NameTypeRequiredDefaultDescription
...values number optional — Zero or more numbers to compare.

Returns

number The largest of the given numbers.

Example

let hi = Math.max(3, 9, 4); // 9

Math.min()

Math.min(...values) → number

Returns the smallest of zero or more numbers.

Returns the smallest of the given numbers. Returns Infinity if no arguments are given, and NaN if any argument is NaN.

Arguments

NameTypeRequiredDefaultDescription
...values number optional — Zero or more numbers to compare.

Returns

number The smallest of the given numbers.

Example

let lo = Math.min(3, 9, 4); // 3

Math.pow()

Math.pow(x, y) → number

Returns base x to the exponent power y.

Returns x raised to the power y (that is, x^y).

Arguments

NameTypeRequiredDefaultDescription
x number optional — The base.
y number optional — The exponent.

Returns

number x raised to the power y.

Example

let p = Math.pow(2, 10); // 1024

Math.round()

Math.round(x) → number

Returns the value of x rounded to the nearest integer.

Rounds x to the nearest integer. Values with a fractional part of exactly 0.5 are rounded up toward positive infinity, so Math.round(-2.5) returns -2.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number x rounded to the nearest integer.

Example

let r = Math.round(4.5); // 5

Math.sin()

Math.sin(x) → number

Returns the sine of x.

Returns the sine of the angle x, given in radians. The result is in the range -1 to 1.

Arguments

NameTypeRequiredDefaultDescription
x number optional — An angle in radians.

Returns

number The sine of x.

Example

let s = Math.sin(Math.PI / 2); // 1

Math.sinh()

Math.sinh(x) → number

Returns the hyperbolic sine of x.

Returns the hyperbolic sine of x.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number The hyperbolic sine of x.

Example

let s = Math.sinh(1); // 1.1752011936438014

Math.sqrt()

Math.sqrt(x) → number

Returns the positive square root of x.

Returns the positive square root of x. Returns NaN for negative values.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number greater than or equal to 0.

Returns

number The positive square root of x.

Example

let s = Math.sqrt(16); // 4

Math.tan()

Math.tan(x) → number

Returns the tangent of x.

Returns the tangent of the angle x, given in radians.

Arguments

NameTypeRequiredDefaultDescription
x number optional — An angle in radians.

Returns

number The tangent of x.

Example

let t = Math.tan(Math.PI / 4); // 0.9999999999999999

Math.tanh()

Math.tanh(x) → number

Returns the hyperbolic tangent of x.

Returns the hyperbolic tangent of x. The result is in the range -1 to 1.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number The hyperbolic tangent of x.

Example

let t = Math.tanh(1); // 0.7615941559557649

Math.trunc()

Math.trunc(x) → number

Returns the integer portion of x, removing any fractional digits.

Removes the fractional part of x, rounding toward zero. Unlike Math.floor(), negative values are rounded up.

Arguments

NameTypeRequiredDefaultDescription
x number optional — A number.

Returns

number The integer part of x.

Example

let t = Math.trunc(-4.7); // -4