API / JavaScript / Js / Math

Math

Provide utilities for JS Math. Note: The constants _E, _LN10, _LN2, _LOG10E, _LOG2E, _PI, _SQRT1_2, and _SQRT2 begin with an underscore because ReasonML variable names cannot begin with a capital letter. (Module names begin with upper case.)

let _E: float;

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

let _LN2: float;

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

let _LN10: float;

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

let _LOG2E: float;

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

let _LOG10E: float;

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

let _PI: float;

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

let _SQRT1_2: float;

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

let _SQRT2: float;

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

let abs_int: int => int;

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

let abs_float: float => float;

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

let acos: float => float;

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

let acosh: float => float;

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

let asin: float => float;

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

let asinh: float => float;

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

let atan: float => float;

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

let atanh: float => float;

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 atan2: (~y: float, ~x: float, unit) => float;

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.

RE
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 cbrt: float => float;

Cube root. See Math.cbrt on MDN

let unsafe_ceil_int: float => 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.

RE
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: float => int;

Deprecated; please use unsafe_ceil_int instead.

let ceil_int: float => 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.

RE
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: float => int;

Deprecated; please use ceil_int instead.

let ceil_float: float => 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.

RE
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 clz32: int => int;

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

RE
Js.Math.clz32(0) == 32; Js.Math.clz32(-1) == 0; Js.Math.clz32(255) == 24;
let cos: float => float;

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

let cosh: float => float;

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

let exp: float => float;

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

let expm1: float => float;

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

let unsafe_floor_int: float => 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.

RE
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: float => int;

Deprecated; please use unsafe_floor_int instead.

let floor_int: float => 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.

RE
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: float => int;

Deprecated; please use floor_int instead.

let floor_float: float => 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.

RE
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 fround: float => float;

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

RE
Js.Math.fround(5.5) == 5.5; Js.Math.fround(5.05) == 5.050000190734863;
let hypot: (float, float) => float;

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

let hypotMany: array(float) => float;

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.

RE
Js.Math.hypotMany([|3.0, 4.0, 12.0|]) == 13.0;
let imul: (int, int) => int;

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 log: float => float;

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.

RE
Js.Math.log(Js.Math._E) == 1.0; Js.Math.log(100.0) == 4.605170185988092;
let log1p: float => float;

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

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

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

RE
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 log2: float => float;

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

RE
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 max_int: (int, int) => int;

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

let maxMany_int: array(int) => int;

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

let max_float: (float, float) => float;

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

let maxMany_float: array(float) => float;

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

let min_int: (int, int) => int;

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

let minMany_int: array(int) => int;

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

let min_float: (float, float) => float;

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

let minMany_float: array(float) => float;

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

let pow_int: (~base: int, ~exp: int) => int;

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

RE
Js.Math.pow_int(~base=3, ~exp=4) == 81;
let pow_float: (~base: float, ~exp: float) => 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.

RE
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 random: unit => float;

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

let random_int: (int, int) => 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 unsafe_round: float => int;

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.

RE
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 round: float => float;

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

let sign_int: int => int;

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

let sign_float: float => 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 sin: float => float;

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

let sinh: float => float;

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

let sqrt: float => float;

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

let tan: float => float;

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 tanh: float => float;

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

let unsafe_trunc: float => int;

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 trunc: float => float;

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