API / Js / Js_math

Js_math

_E

Euler's number; ≈ 2.718281828459045. See Math.E on MDN.

let _E: float

_LN2

Natural logarithm of 2; ≈ 0.6931471805599453. See Math.LN2 on MDN.

let _LN2: float

_LN10

Natural logarithm of 10; ≈ 2.302585092994046. See Math.LN10 on MDN.

let _LN10: float

_LOG2E

Base 2 logarithm of E; ≈ 1.4426950408889634. See Math.LOG2E on MDN.

let _LOG2E: float

_LOG10E

Base 10 logarithm of E; ≈ 0.4342944819032518. See Math.LOG10E on MDN.

let _LOG10E: float

_PI

Pi - ratio of the circumference to the diameter of a circle; ≈ 3.141592653589793. See Math.PI on MDN.

let _PI: float

_SQRT1_2

Square root of 1/2; ≈ 0.7071067811865476. See Math.SQRT1_2 on MDN.

let _SQRT1_2: float

_SQRT2

Square root of 2; ≈ 1.4142135623730951. See Math.SQRT2 on MDN.

let _SQRT2: float

abs_int

Absolute value for integer argument. See Math.abs on MDN.

let abs_int: int => int

abs_float

Absolute value for float argument. See Math.abs on MDN.

let abs_float: float => float

acos

Arccosine (in radians) of argument; returns NaN if the argument is outside the range [-1.0, 1.0]. See Math.acos on MDN.

let acos: float => float

acosh

Hyperbolic arccosine (in radians) of argument; returns NaN if the argument is less than 1.0. See Math.acosh on MDN.

let acosh: float => float

asin

Arcsine (in radians) of argument; returns NaN if the argument is outside the range [-1.0, 1.0]. See Math.asin on MDN.

let asin: float => float

asinh

Hyperbolic arcsine (in radians) of argument. See Math.asinh on MDN.

let asinh: float => float

atan

Arctangent (in radians) of argument. See Math.atan on MDN.

let atan: float => float

atanh

Hyperbolic arctangent (in radians) of argument; returns NaN if the argument is is outside the range [-1.0, 1.0]. Returns -Infinity and Infinity for arguments -1.0 and 1.0. See Math.atanh on MDN.

let atanh: float => float

atan2

Returns the angle (in radians) of the quotient y /. x. It is also the angle between the x-axis and point (x, y). See Math.atan2 on MDN.

RES
Js.Math.atan2(~y=0.0, ~x=10.0, ()) == 0.0 Js.Math.atan2(~x=5.0, ~y=5.0, ()) == Js.Math._PI /. 4.0 Js.Math.atan2(~x=-5.0, ~y=5.0, ()) Js.Math.atan2(~x=-5.0, ~y=5.0, ()) == 3.0 *. Js.Math._PI /. 4.0 Js.Math.atan2(~x=-0.0, ~y=-5.0, ()) == -.Js.Math._PI /. 2.0
let atan2: (~y: float, ~x: float, unit) => float

cbrt

Cube root. See Math.cbrt on MDN

let cbrt: float => float

unsafe_ceil_int

Returns the smallest integer greater than or equal to the argument. This function may return values not representable by int, whose range is -2147483648 to 2147483647. This is because, in JavaScript, there are only 64-bit floating point numbers, which can represent integers in the range ±(253-1) exactly. See Math.ceil on MDN.

RES
Js.Math.unsafe_ceil_int(3.1) == 4 Js.Math.unsafe_ceil_int(3.0) == 3 Js.Math.unsafe_ceil_int(-3.1) == -3 Js.Math.unsafe_ceil_int(1.0e15) // result is outside range of int datatype
let unsafe_ceil_int: float => int

unsafe_ceil

Deprecated; please use unsafe_ceil_int instead.

let unsafe_ceil: float => int

ceil_int

Returns the smallest int greater than or equal to the argument; the result is pinned to the range of the int data type: -2147483648 to 2147483647. See Math.ceil on MDN.

RES
Js.Math.ceil_int(3.1) == 4 Js.Math.ceil_int(3.0) == 3 Js.Math.ceil_int(-3.1) == -3 Js.Math.ceil_int(-1.0e15) == -2147483648 Js.Math.ceil_int(1.0e15) == 2147483647
let ceil_int: float => int

ceil

Deprecated; please use ceil_int instead.

let ceil: float => int

ceil_float

Returns the smallest integral value greater than or equal to the argument. The result is a float and is not restricted to the int data type range. See Math.ceil on MDN.

RES
Js.Math.ceil_float(3.1) == 4.0 Js.Math.ceil_float(3.0) == 3.0 Js.Math.ceil_float(-3.1) == -3.0 Js.Math.ceil_float(2_150_000_000.3) == 2_150_000_001.0
let ceil_float: float => float

clz32

Number of leading zero bits of the argument's 32 bit int representation. See Math.clz32 on MDN.

RES
Js.Math.clz32(0) == 32 Js.Math.clz32(-1) == 0 Js.Math.clz32(255) == 24
let clz32: int => int

cos

Cosine of argument, which must be specified in radians. See Math.cos on MDN.

let cos: float => float

cosh

Hyperbolic cosine of argument, which must be specified in radians. See Math.cosh on MDN.

let cosh: float => float

exp

Natural exponentional; returns e (the base of natural logarithms) to the power of the given argument. See Math.exp on MDN.

let exp: float => float

expm1

Returns e (the base of natural logarithms) to the power of the given argument minus 1. See Math.expm1 on MDN.

let expm1: float => float

unsafe_floor_int

Returns the largest integer less than or equal to the argument. This function may return values not representable by int, whose range is -2147483648 to 2147483647. This is because, in JavaScript, there are only 64-bit floating point numbers, which can represent integers in the range ±(253-1) exactly. See Math.floor on MDN.

RES
Js.Math.unsafe_floor_int(3.7) == 3 Js.Math.unsafe_floor_int(3.0) == 3 Js.Math.unsafe_floor_int(-3.7) == -4 Js.Math.unsafe_floor_int(1.0e15) // result is outside range of int datatype
let unsafe_floor_int: float => int

unsafe_floor

Deprecated; please use unsafe_floor_int instead.

let unsafe_floor: float => int

floor_int

Returns the largest int less than or equal to the argument; the result is pinned to the range of the int data type: -2147483648 to 2147483647. See Math.floor on MDN.

RES
Js.Math.floor_int(3.7) == 3 Js.Math.floor_int(3.0) == 3 Js.Math.floor_int(-3.1) == -4 Js.Math.floor_int(-1.0e15) == -2147483648 Js.Math.floor_int(1.0e15) == 2147483647
let floor_int: float => int

floor

Deprecated; please use floor_int instead.

let floor: float => int

floor_float

Returns the largest integral value less than or equal to the argument. The result is a float and is not restricted to the int data type range. See Math.floor on MDN.

RES
Js.Math.floor_float(3.7) == 3.0 Js.Math.floor_float(3.0) == 3.0 Js.Math.floor_float(-3.1) == -4.0 Js.Math.floor_float(2_150_000_000.3) == 2_150_000_000.0
let floor_float: float => float

fround

Round to nearest single precision float. See Math.fround on MDN.

RES
Js.Math.fround(5.5) == 5.5 Js.Math.fround(5.05) == 5.050000190734863
let fround: float => float

hypot

Returns the square root of the sum of squares of its two arguments (the Pythagorean formula). See Math.hypot on MDN.

let hypot: (float, float) => float

hypotMany

Returns the square root of the sum of squares of the numbers in the array argument (generalized Pythagorean equation). Using an array allows you to have more than two items. See Math.hypot on MDN.

RES
Js.Math.hypotMany([3.0, 4.0, 12.0]) == 13.0
let hypotMany: array<float> => float

imul

32-bit integer multiplication. Use this only when you need to optimize performance of multiplication of numbers stored as 32-bit integers. See Math.imul on MDN.

let imul: (int, int) => int

log

Returns the natural logarithm of its argument; this is the number x such that ex equals the argument. Returns NaN for negative arguments. See Math.log on MDN.

RES
Js.Math.log(Js.Math._E) == 1.0 Js.Math.log(100.0) == 4.605170185988092
let log: float => float

log1p

Returns the natural logarithm of one plus the argument. Returns NaN for arguments less than -1. See Math.log1p on MDN.

RES
Js.Math.log1p(Js.Math._E -. 1.0) == 1.0 Js.Math.log1p(99.0) == 4.605170185988092
let log1p: float => float

log10

Returns the base 10 logarithm of its argument. Returns NaN for negative arguments. See Math.log10 on MDN.

RES
Js.Math.log10(1000.0) == 3.0 Js.Math.log10(0.01) == -2.0 Js.Math.log10(Js.Math.sqrt(10.0)) == 0.5
let log10: float => float

log2

Returns the base 2 logarithm of its argument. Returns NaN for negative arguments. See Math.log2 on MDN.

RES
Js.Math.log2(512.0) == 9.0 Js.Math.log2(0.125) == -3.0 Js.Math.log2(Js.Math._SQRT2) == 0.5000000000000001 // due to precision
let log2: float => float

max_int

Returns the maximum of its two integer arguments. See Math.max on MDN.

let max_int: (int, int) => int

maxMany_int

Returns the maximum of the integers in the given array. See Math.max on MDN.

let maxMany_int: array<int> => int

max_float

Returns the maximum of its two floating point arguments. See Math.max on MDN.

let max_float: (float, float) => float

maxMany_float

Returns the maximum of the floating point values in the given array. See Math.max on MDN.

let maxMany_float: array<float> => float

min_int

Returns the minimum of its two integer arguments. See Math.min on MDN.

let min_int: (int, int) => int

minMany_int

Returns the minimum of the integers in the given array. See Math.min on MDN.

let minMany_int: array<int> => int

min_float

Returns the minimum of its two floating point arguments. See Math.min on MDN.

let min_float: (float, float) => float

minMany_float

Returns the minimum of the floating point values in the given array. See Math.min on MDN.

let minMany_float: array<float> => float

pow_int

Raises the given base to the given exponent. (Arguments and result are integers.) See Math.pow on MDN.

RES
Js.Math.pow_int(~base=3, ~exp=4) == 81
let pow_int: (~base: int, ~exp: int) => int

pow_float

Raises the given base to the given exponent. (Arguments and result are floats.) Returns NaN if the result would be imaginary. See Math.pow on MDN.

RES
Js.Math.pow_float(~base=3.0, ~exp=4.0) == 81.0 Js.Math.pow_float(~base=4.0, ~exp=-2.0) == 0.0625 Js.Math.pow_float(~base=625.0, ~exp=0.5) == 25.0 Js.Math.pow_float(~base=625.0, ~exp=-0.5) == 0.04 Js.Float.isNaN(Js.Math.pow_float(~base=-2.0, ~exp=0.5)) == true
let pow_float: (~base: float, ~exp: float) => float

random

Returns a random number in the half-closed interval [0,1). See Math.random on MDN.

let random: unit => float

random_int

A call to random_int(minVal, maxVal) returns a random number in the half-closed interval [minVal, maxVal). See Math.random on MDN.

let random_int: (int, int) => int

unsafe_round

Rounds its argument to nearest integer. For numbers with a fractional portion of exactly 0.5, the argument is rounded to the next integer in the direction of positive infinity. This function may return values not representable by int, whose range is -2147483648 to 2147483647. This is because, in JavaScript, there are only 64-bit floating point numbers, which can represent integers in the range ±(253-1) exactly. See Math.round on MDN.

RES
Js.Math.unsafe_round(3.7) == 4 Js.Math.unsafe_round(-3.5) == -3 Js.Math.unsafe_round(2_150_000_000_000.3) // out of range for int
let unsafe_round: float => int

round

Rounds to nearest integral value (expressed as a float). See Math.round on MDN.

let round: float => float

sign_int

Returns the sign of its integer argument: -1 if negative, 0 if zero, 1 if positive. See Math.sign on MDN.

let sign_int: int => int

sign_float

Returns the sign of its float argument: -1.0 if negative, 0.0 if zero, 1.0 if positive. See Math.sign on MDN.

let sign_float: float => float

sin

Sine of argument, which must be specified in radians. See Math.sin on MDN.

let sin: float => float

sinh

Hyperbolic sine of argument, which must be specified in radians. See Math.sinh on MDN.

let sinh: float => float

sqrt

Square root. If the argument is negative, this function returns NaN. See Math.sqrt on MDN.

let sqrt: float => float

tan

Tangent of argument, which must be specified in radians. Returns NaN if the argument is positive infinity or negative infinity. See Math.cos on MDN.

let tan: float => float

tanh

Hyperbolic tangent of argument, which must be specified in radians. See Math.tanh on MDN.

let tanh: float => float

unsafe_trunc

Truncates its argument; i.e., removes fractional digits. This function may return values not representable by int, whose range is -2147483648 to 2147483647. This is because, in JavaScript, there are only 64-bit floating point numbers, which can represent integers in the range ±(253-1) exactly. See Math.trunc on MDN.

let unsafe_trunc: float => int

trunc

Truncates its argument; i.e., removes fractional digits. See Math.trunc on MDN.

let trunc: float => float