Math

Functions for doing math

round : Float -> Int

Round a number to the nearest integer.

round 1.0 == 1

round 1.2 == 1

round 1.5 == 2

round 1.8 == 2

round -1.2 == -1

round -1.5 == -1

round -1.8 == -2
floor : Float -> Int

Floor function, rounding down.

floor 1.0 == 1

floor 1.2 == 1

floor 1.5 == 1

floor 1.8 == 1

floor -1.2 == -2

floor -1.5 == -2

floor -1.8 == -2
ceiling : Float -> Int

Ceiling function, rounding up.

ceiling 1.0 == 1

ceiling 1.2 == 2

ceiling 1.5 == 2

ceiling 1.8 == 2

ceiling -1.2 == -1

ceiling -1.5 == -1

ceiling -1.8 == -1
truncate : Float -> Int

Truncate a number, rounding towards zero.

truncate 1.0 == 1

truncate 1.2 == 1

truncate 1.5 == 1

truncate 1.8 == 1

truncate -1.2 == -1

truncate -1.5 == -1

truncate -1.8 == -1
modBy : Int -> Int -> Int

Perform modular arithmetic. A common trick is to use (n mod 2) to detect even and odd numbers:

modBy 2 0 == 0

modBy 2 1 == 1

modBy 2 2 == 0

modBy 2 3 == 1

Our modBy function works in the typical mathematical way when you run into negative numbers:

List.map (modBy 4) [ -5, -4, -3, -2, -1,  0,  1,  2,  3,  4,  5 ]
--                 [  3,  0,  1,  2,  3,  0,  1,  2,  3,  0,  1 ]

Use remainderBy for a different treatment of negative numbers, or read Daan Leijen’s Division and Modulus for Computer Scientists for more information.

remainderBy : Int -> Int -> Int

Get the remainder after division. Here are bunch of examples of dividing by four:

List.map (remainderBy 4) [ -5, -4, -3, -2, -1,  0,  1,  2,  3,  4,  5 ]
--                       [ -1,  0, -3, -2, -1,  0,  1,  2,  3,  0,  1 ]

Use modBy for a different treatment of negative numbers, or read Daan Leijen’s Division and Modulus for Computer Scientists for more information.

abs : number -> number

Get the absolute value of a number.

abs 16 == 16

abs -4 == 4

abs -8.5 == 8.5

abs 3.14 == 3.14
sqrt : Float -> Float

Take the square root of a number.

sqrt 4 == 2

sqrt 9 == 3

sqrt 16 == 4

sqrt 25 == 5
logBase : Float -> Float -> Float

Calculate the logarithm of a number with a given base.

logBase 10 100 == 2

logBase 2 256 == 8

Constants

e : Float

An approximation of e.

pi : Float

An approximation of pi.

maxSafeInteger : Int

The largest integer value that can be exactly represented and compared in a JavaScript environment. Integers above this value may not work as you expect.

minSafeInteger : Int

The smallest integer value that can be exactly represented and compared in a JavaScript environment. Integers below this value may not work as you expect.

maxFloat : Float

The largest Float value.

minFloat : Float

The smallest Float value.

Angles

degrees : Float -> Float

Convert degrees to standard Gren angles (radians).

degrees 180 == 3.141592653589793
radians : Float -> Float

Convert radians to standard Gren angles (radians).

radians pi == 3.141592653589793
turns : Float -> Float

Convert turns to standard Gren angles (radians). One turn is equal to 360°.

turns (1 / 2) == 3.141592653589793

Trigonometry

cos : Float -> Float

Figure out the cosine given an angle in radians.

cos (degrees 60) == 0.5000000000000001

cos (turns (1 / 6)) == 0.5000000000000001

cos (radians (pi / 3)) == 0.5000000000000001

cos (pi / 3) == 0.5000000000000001
sin : Float -> Float

Figure out the sine given an angle in radians.

sin (degrees 30) == 0.49999999999999994

sin (turns (1 / 12)) == 0.49999999999999994

sin (radians (pi / 6)) == 0.49999999999999994

sin (pi / 6) == 0.49999999999999994
tan : Float -> Float

Figure out the tangent given an angle in radians.

tan (degrees 45) == 0.9999999999999999

tan (turns (1 / 8)) == 0.9999999999999999

tan (radians (pi / 4)) == 0.9999999999999999

tan (pi / 4) == 0.9999999999999999
acos : Float -> Float

Figure out the arccosine for adjacent / hypotenuse in radians:

acos (1 / 2) == 1.0471975511965979 -- 60° or pi/3 radians
asin : Float -> Float

Figure out the arcsine for opposite / hypotenuse in radians:

asin (1 / 2) == 0.5235987755982989 -- 30° or pi/6 radians
atan : Float -> Float

This helps you find the angle (in radians) to an (x,y) coordinate, but in a way that is rarely useful in programming. You probably want atan2 instead!

This version takes y/x as its argument, so there is no way to know whether the negative signs comes from the y or x value. So as we go counter-clockwise around the origin from point (1,1) to (1,-1) to (-1,-1) to (-1,1) we do not get angles that go in the full circle:

atan (1 / 1) == 0.7853981633974483 --  45° or   pi/4 radians

atan (1 / -1) == -0.7853981633974483 -- 315° or 7*pi/4 radians

atan (-1 / -1) == 0.7853981633974483 --  45° or   pi/4 radians

atan (-1 / 1) == -0.7853981633974483 -- 315° or 7*pi/4 radians

Notice that everything is between pi/2 and -pi/2. That is pretty useless for figuring out angles in any sort of visualization, so again, check out atan2 instead!

atan2 : Float -> Float -> Float

This helps you find the angle (in radians) to an (x,y) coordinate. So rather than saying atan (y/x) you say atan2 y x and you can get a full range of angles:

atan2 1 1 == 0.7853981633974483 --  45° or   pi/4 radians

atan2 1 -1 == 2.356194490192345 -- 135° or 3*pi/4 radians

atan2 -1 -1 == -2.356194490192345 -- 225° or 5*pi/4 radians

atan2 -1 1 == -0.7853981633974483 -- 315° or 7*pi/4 radians