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.)
_E
RESlet _E: float
Euler's number; ≈ 2.718281828459045. See Math.E
on MDN.
_LN2
RESlet _LN2: float
Natural logarithm of 2; ≈ 0.6931471805599453. See Math.LN2
on MDN.
_LN10
RESlet _LN10: float
Natural logarithm of 10; ≈ 2.302585092994046. See Math.LN10
on MDN.
_LOG2E
RESlet _LOG2E: float
Base 2 logarithm of E; ≈ 1.4426950408889634. See Math.LOG2E
on MDN.
_LOG10E
RESlet _LOG10E: float
Base 10 logarithm of E; ≈ 0.4342944819032518. See Math.LOG10E
on MDN.
_PI
RESlet _PI: float
Pi - ratio of the circumference to the diameter of a circle; ≈ 3.141592653589793. See Math.PI
on MDN.
_SQRT1_2
RESlet _SQRT1_2: float
Square root of 1/2; ≈ 0.7071067811865476. See Math.SQRT1_2
on MDN.
_SQRT2
RESlet _SQRT2: float
Square root of 2; ≈ 1.4142135623730951. See Math.SQRT2
on MDN.
abs_int
RESlet abs_int: int => int
Absolute value for integer argument. See Math.abs
on MDN.
abs_float
RESlet abs_float: float => float
Absolute value for float argument. See Math.abs
on MDN.
acos
RESlet 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
RESlet 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
RESlet 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
RESlet asinh: float => float
Hyperbolic arcsine (in radians) of argument. See Math.asinh
on MDN.
atan
RESlet atan: float => float
Arctangent (in radians) of argument. See Math.atan
on MDN.
atanh
RESlet 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
RESlet 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.
RESJs.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
RESlet cbrt: float => float
Cube root. See Math.cbrt
on MDN
unsafe_ceil_int
RESlet 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.
RESJs.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
RESlet unsafe_ceil: float => int
Deprecated; please use unsafe_ceil_int
instead.
ceil_int
RESlet 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.
RESJs.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
RESlet ceil: float => int
Deprecated; please use ceil_int
instead.
ceil_float
RESlet 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.
RESJs.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
RESlet clz32: int => int
Number of leading zero bits of the argument's 32 bit int representation. See Math.clz32
on MDN.
RESJs.Math.clz32(0) == 32
Js.Math.clz32(-1) == 0
Js.Math.clz32(255) == 24
cos
RESlet cos: float => float
Cosine of argument, which must be specified in radians. See Math.cos
on MDN.
cosh
RESlet cosh: float => float
Hyperbolic cosine of argument, which must be specified in radians. See Math.cosh
on MDN.
exp
RESlet 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
RESlet 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
RESlet 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.
RESJs.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
RESlet unsafe_floor: float => int
Deprecated; please use unsafe_floor_int
instead.
floor_int
RESlet 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.
RESJs.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
RESlet floor: float => int
Deprecated; please use floor_int
instead.
floor_float
RESlet 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.
RESJs.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
RESlet fround: float => float
Round to nearest single precision float. See Math.fround
on MDN.
RESJs.Math.fround(5.5) == 5.5
Js.Math.fround(5.05) == 5.050000190734863
hypot
RESlet 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
RESlet 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.
RESJs.Math.hypotMany([3.0, 4.0, 12.0]) == 13.0
imul
RESlet 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
RESlet 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.
RESJs.Math.log(Js.Math._E) == 1.0
Js.Math.log(100.0) == 4.605170185988092
log1p
RESlet log1p: float => float
Returns the natural logarithm of one plus the argument. Returns NaN
for arguments less than -1. See Math.log1p
on MDN.
RESJs.Math.log1p(Js.Math._E -. 1.0) == 1.0
Js.Math.log1p(99.0) == 4.605170185988092
log10
RESlet log10: float => float
Returns the base 10 logarithm of its argument. Returns NaN
for negative arguments. See Math.log10
on MDN.
RESJs.Math.log10(1000.0) == 3.0
Js.Math.log10(0.01) == -2.0
Js.Math.log10(Js.Math.sqrt(10.0)) == 0.5
log2
RESlet log2: float => float
Returns the base 2 logarithm of its argument. Returns NaN
for negative arguments. See Math.log2
on MDN.
RESJs.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
RESlet max_int: (int, int) => int
Returns the maximum of its two integer arguments. See Math.max
on MDN.
maxMany_int
RESlet maxMany_int: array<int> => int
Returns the maximum of the integers in the given array. See Math.max
on MDN.
max_float
RESlet max_float: (float, float) => float
Returns the maximum of its two floating point arguments. See Math.max
on MDN.
maxMany_float
RESlet maxMany_float: array<float> => float
Returns the maximum of the floating point values in the given array. See Math.max
on MDN.
min_int
RESlet min_int: (int, int) => int
Returns the minimum of its two integer arguments. See Math.min
on MDN.
minMany_int
RESlet minMany_int: array<int> => int
Returns the minimum of the integers in the given array. See Math.min
on MDN.
min_float
RESlet min_float: (float, float) => float
Returns the minimum of its two floating point arguments. See Math.min
on MDN.
minMany_float
RESlet minMany_float: array<float> => float
Returns the minimum of the floating point values in the given array. See Math.min
on MDN.
pow_int
RESlet 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.
RESJs.Math.pow_int(~base=3, ~exp=4) == 81
pow_float
RESlet 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.
RESJs.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
RESlet random: unit => float
Returns a random number in the half-closed interval [0,1). See Math.random
on MDN.
random_int
RESlet 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
RESlet 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.
RESJs.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
RESlet round: float => float
Rounds to nearest integral value (expressed as a float). See Math.round
on MDN.
sign_int
RESlet 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
RESlet 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
RESlet sin: float => float
Sine of argument, which must be specified in radians. See Math.sin
on MDN.
sinh
RESlet sinh: float => float
Hyperbolic sine of argument, which must be specified in radians. See Math.sinh
on MDN.
sqrt
RESlet sqrt: float => float
Square root. If the argument is negative, this function returns NaN
. See Math.sqrt
on MDN.
tan
RESlet 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
RESlet tanh: float => float
Hyperbolic tangent of argument, which must be specified in radians. See Math.tanh
on MDN.
unsafe_trunc
RESlet 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.
trunc
RESlet trunc: float => float
Truncates its argument; i.e., removes fractional digits. See Math.trunc
on MDN.