Math
Provide utilities for JS Math. Note: The constants _E
, _LN10
, _LN2
,
_LOG10E
, _LOG2E
, _PI
, _SQRT1_2
, and _SQRT2
begin with an underscore
because ReScript variable names cannot begin with a capital letter. (Module
names begin with upper case.)
_E
let _E: float
Euler's number; ≈ 2.718281828459045. See
Math.E
on MDN.
_LN2
let _LN2: float
Natural logarithm of 2; ≈ 0.6931471805599453. See
Math.LN2
on MDN.
_LN10
let _LN10: float
Natural logarithm of 10; ≈ 2.302585092994046. See
Math.LN10
on MDN.
_LOG2E
let _LOG2E: float
Base 2 logarithm of E; ≈ 1.4426950408889634. See
Math.LOG2E
on MDN.
_LOG10E
let _LOG10E: float
Base 10 logarithm of E; ≈ 0.4342944819032518. See
Math.LOG10E
on MDN.
_PI
let _PI: float
Pi - ratio of the circumference to the diameter of a circle; ≈ 3.141592653589793. See
Math.PI
on MDN.
_SQRT1_2
let _SQRT1_2: float
Square root of 1/2; ≈ 0.7071067811865476. See
Math.SQRT1_2
on MDN.
_SQRT2
let _SQRT2: float
Square root of 2; ≈ 1.4142135623730951. See
Math.SQRT2
on MDN.
abs_int
let abs_int: int => int
Absolute value for integer argument. See
Math.abs
on MDN.
abs_float
let abs_float: float => float
Absolute value for float argument. See
Math.abs
on MDN.
acos
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.
acosh
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.
asin
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.
asinh
let asinh: float => float
Hyperbolic arcsine (in radians) of argument. See
Math.asinh
on MDN.
atan
let atan: float => float
Arctangent (in radians) of argument. See
Math.atan
on MDN.
atanh
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.
atan2
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.
Examples
RESCRIPTJs.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
cbrt
let cbrt: float => float
Cube root. See
Math.cbrt
on MDN
unsafe_ceil_int
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
±(2<sup>53</sup>-1) exactly. See
Math.ceil
on MDN.
Examples
RESCRIPTJs.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
unsafe_ceil
Deprecated
Please use unsafe_ceil_int
instead
let unsafe_ceil: float => int
ceil_int
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.
Examples
RESCRIPTJs.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
ceil
Deprecated
Please use ceil_int
instead
let ceil: float => int
ceil_float
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.
Examples
RESCRIPTJs.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
clz32
let clz32: int => int
Number of leading zero bits of the argument's 32 bit int representation. See
Math.clz32
on MDN.
Examples
RESCRIPTJs.Math.clz32(0) == 32
Js.Math.clz32(-1) == 0
Js.Math.clz32(255) == 24
cos
let cos: float => float
Cosine of argument, which must be specified in radians. See
Math.cos
on MDN.
cosh
let cosh: float => float
Hyperbolic cosine of argument, which must be specified in radians. See
Math.cosh
on MDN.
exp
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.
expm1
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.
unsafe_floor_int
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
±(2<sup>53</sup>-1) exactly. See
Math.floor
on MDN.
Examples
RESCRIPTJs.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
unsafe_floor
Deprecated
Please use unsafe_floor_int
instead
let unsafe_floor: float => int
floor_int
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.
Examples
RESCRIPTJs.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
floor
Deprecated
Please use floor_int
instead
let floor: float => int
floor_float
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.
Examples
RESCRIPTJs.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
fround
let fround: float => float
Round to nearest single precision float. See
Math.fround
on MDN.
Examples
RESCRIPTJs.Math.fround(5.5) == 5.5
Js.Math.fround(5.05) == 5.050000190734863
hypot
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.
hypotMany
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.
Examples
RESCRIPTJs.Math.hypotMany([3.0, 4.0, 12.0]) == 13.0
imul
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.
log
let log: float => float
Returns the natural logarithm of its argument; this is the number x such
that e<sup>x</sup> equals the argument. Returns NaN
for negative
arguments. See
Math.log
on MDN.
Examples
RESCRIPTJs.Math.log(Js.Math._E) == 1.0
Js.Math.log(100.0) == 4.605170185988092
log1p
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.
Examples
RESCRIPTJs.Math.log1p(Js.Math._E -. 1.0) == 1.0
Js.Math.log1p(99.0) == 4.605170185988092
log10
let log10: float => float
Returns the base 10 logarithm of its argument. Returns NaN
for negative
arguments. See
Math.log10
on MDN.
Examples
RESCRIPTJs.Math.log10(1000.0) == 3.0
Js.Math.log10(0.01) == -2.0
Js.Math.log10(Js.Math.sqrt(10.0)) == 0.5
log2
let log2: float => float
Returns the base 2 logarithm of its argument. Returns NaN
for negative
arguments. See
Math.log2
on MDN.
Examples
RESCRIPTJs.Math.log2(512.0) == 9.0
Js.Math.log2(0.125) == -3.0
Js.Math.log2(Js.Math._SQRT2) == 0.5000000000000001 // due to precision
max_int
let max_int: (int, int) => int
Returns the maximum of its two integer arguments. See
Math.max
on MDN.
maxMany_int
let maxMany_int: array<int> => int
Returns the maximum of the integers in the given array. See
Math.max
on MDN.
max_float
let max_float: (float, float) => float
Returns the maximum of its two floating point arguments. See
Math.max
on MDN.
maxMany_float
let maxMany_float: array<float> => float
Returns the maximum of the floating point values in the given array. See
Math.max
on MDN.
min_int
let min_int: (int, int) => int
Returns the minimum of its two integer arguments. See
Math.min
on MDN.
minMany_int
let minMany_int: array<int> => int
Returns the minimum of the integers in the given array. See
Math.min
on MDN.
min_float
let min_float: (float, float) => float
Returns the minimum of its two floating point arguments. See
Math.min
on MDN.
minMany_float
let minMany_float: array<float> => float
Returns the minimum of the floating point values in the given array. See
Math.min
on MDN.
pow_int
Deprecated
use pow_float
instead, the return type may be not int
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.
Examples
RESCRIPTJs.Math.pow_int(~base=3, ~exp=4) == 81
pow_float
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.
Examples
RESCRIPTJs.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
random
let random: unit => float
Returns a random number in the half-closed interval [0,1). See
Math.random
on MDN.
random_int
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.
unsafe_round
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 ±(2<sup>53</sup>-1) exactly. See
Math.round
on MDN.
Examples
RESCRIPTJs.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
round
let round: float => float
Rounds to nearest integral value (expressed as a float). See
Math.round
on MDN.
sign_int
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.
sign_float
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.
sin
let sin: float => float
Sine of argument, which must be specified in radians. See
Math.sin
on MDN.
sinh
let sinh: float => float
Hyperbolic sine of argument, which must be specified in radians. See
Math.sinh
on MDN.
sqrt
let sqrt: float => float
Square root. If the argument is negative, this function returns NaN
. See
Math.sqrt
on MDN.
tan
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.
tanh
let tanh: float => float
Hyperbolic tangent of argument, which must be specified in radians. See
Math.tanh
on MDN.
unsafe_trunc
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 ±(2<sup>53</sup>-1)
exactly. See
Math.trunc
on MDN.
trunc
let trunc: float => float
Truncates its argument; i.e., removes fractional digits. See
Math.trunc
on MDN.