module Math:sig
..end
Math
contains many common mathematical functions.
val abs : int -> int
abs i
is the absolute value of i
.
val absf : float -> float
absf f
is the absolute value of f
.
val ceil : float -> int
ceil f
is the integer closest to f
in the direction of positive
infinity.
val floor : float -> int
floor f
is the integer closest to f
in the direction of negative
infinity.
val constrain : int -> int -> int -> int
constrain i min max
is min
if i <= min
, max
if i >= max
,
or i
otherwise. The behavior is undefined if min > max
.
val constrainf : float -> float -> float -> float
constrainf f min max
is min
if f <= min
, max
if f >= max
,
or f
otherwise. The behavior is undefined if min > max
.
val dist : int -> int -> int -> int -> float
dist x1 y1 x2 y2
is the distance between the points (x1, y1)
and
(x2, y2)
.
val distf : float -> float -> float -> float -> float
distf x1 y1 x2 y2
is the distance between the points (x1, y1)
and
(x2, y2)
.
val mag : int -> int -> float
mag x y
is dist x y 0 0
.
val magf : float -> float -> float
magf x y
is distf x y 0 0
.
val lerp : int -> int -> float -> int
lerp a b t
is the linear interpolation from a
to b
by t
, where
t
is in [0, 1]
.
val lerpf : float -> float -> float -> float
lerpf a b t
is the linear interpolation from a
to b
by t
, where
t
is in [0, 1]
.
val log : float -> float -> float
log f b
is the base-b
logarithm of f
.
val map : int -> int -> int -> int -> int -> int
map i low1 high1 low2 high2
is i
mapped from the range [low1, high1]
to the range [low2, high2]
.
val mapf : float -> float -> float -> float -> float -> float
mapf f low1 high1 low2 high2
is f
mapped from the range
[low1, high1]
to the range [low2, high2]
.
val max : int -> int -> int
max a b
is the maximum of a
and b
.
val maxf : float -> float -> float
maxf a b
is the maximum of a
and b
.
val min : int -> int -> int
min a b
is the minimum of a
and b
.
val minf : float -> float -> float
minf a b
is the minimum of a
and b
.
val norm : int -> int -> int -> float
norm i low high
is map i low high 0 1
.
val normf : float -> float -> float -> float
normf f low high
is mapf f low high 0. 1.
.
val round : float -> int
round f
is the integer closest to f
; 0.5
rounds up.
val sqrt : float -> float
sqrt f
is the square root of f
.
All functions in this section expect angles in radians.
val acos : float -> float
acos f
is the inverse cosine of f
. f
is in [-1, 1]
and acos f
is in [0, pi]
.
val asin : float -> float
asin f
is the inverse sine of f
. f
is in [-1, 1]
and
asin f
is in [-pi/2, pi/2]
.
val atan : float -> float
atan f
is the inverse tangent of f
. f
is in
[-infinity, infinity]
and atan f
is in [-pi/2, pi/2]
.
val atan2 : float -> float -> float
atan2 y x
is the angle from the origin to (y, x)
. This function
uses the signs of x
and y
to determine the correct quadrant.
val cos : float -> float
cos theta
is the cosine of theta
.
val sin : float -> float
sin theta
is the sine of theta
.
val tan : float -> float
tan theta
is the tangent of theta
.
val degrees : float -> float
degrees theta
is theta
, which is in radians, converted to degrees.
val radians : float -> float
radians d
is d
, which is in degrees, converted to radians.
All functions in this section expect angles in radians.
val angle_avg : float -> float -> float
angle_avg theta1 theta2
is the average angle between theta1
and
theta2
.
val angle_sum : float -> float -> float
angle_sum theta1 theta2
is the sum of the angles theta1
and
theta2
.
val angle_diff : float -> float -> float
angle_diff theta1 theta2
is the difference between the angles theta1
and theta2
.
val pi : float
pi
is the constant pi. pi = 3.1415926535...
val half_pi : float
half_pi
is pi /. 2.
. half_pi = 1.5707963267...
val two_pi : float
two_pi
is pi *. 2.
. two_pi = 6.2831853071...
val e : float
e
is the constant e. e = 2.7182818284...
val random_int : ?lower_bound:int -> int -> int
random_int bound
is a random integer between 0
(inclusive) and
bound
(exclusive).
val random_float : ?lower_bound:float -> float -> float
random_float bound
is a random float between 0.
(inclusive) and
bound
(exclusive).
val random_bool : unit -> bool
random_bool ()
is either true
or false
with an equal probability
of either.