Skip to content
heff

std.math

The functions of a float: the trigonometric ones (of an angle in radians), the exponential and the logarithm, powers, rounding, and the constants pi and e (sqrt and floor are the prelude’s), and the float a decimal is, correctly rounded (fromDecimal; a Float32’s is float32’s). The primitives are JavaScript’s Math and, in C, the libm’s (sin, cos, tan, exp and log fdlibm’s, as V8 has them, where zig’s compiler_rt would give its own). They may differ in the last place (macOS’s libm and a libm’s asin or pow are their own; V8 for arm64 fuses some of fdlibm’s steps): a program whose output must be the same on each backend shows fewer places than a float has.

floatSin

floatSin : Float64 -> Float64

floatCos

floatCos : Float64 -> Float64

floatTan

floatTan : Float64 -> Float64

floatAsin

floatAsin : Float64 -> Float64

floatAcos

floatAcos : Float64 -> Float64

floatAtan

floatAtan : Float64 -> Float64

floatAtan2

floatAtan2 : Float64 -> Float64 -> Float64

floatExp

floatExp : Float64 -> Float64

floatLog

floatLog : Float64 -> Float64

floatPow

floatPow : Float64 -> Float64 -> Float64

pi

pi : Float64

π, a circle’s circumference over its diameter.

e

e : Float64

e, the base of the natural logarithm.

sin

sin : Float64 -> Float64

The sine of an angle in radians.

>>> sin 0.0
0.0

cos

cos : Float64 -> Float64

The cosine of an angle in radians.

>>> cos pi
-1.0

tan

tan : Float64 -> Float64

The tangent of an angle in radians.

>>> tan 0.0
0.0

asin

asin : Float64 -> Float64

The angle whose sine is x, from -π/2 to π/2.

>>> asin 1.0
1.5707963267948966

acos

acos : Float64 -> Float64

The angle whose cosine is x, from 0 to π.

>>> acos (-1.0)
3.141592653589793

atan

atan : Float64 -> Float64

The angle whose tangent is x, from -π/2 to π/2.

>>> atan 1.0
0.7853981633974483

atan2

atan2 : Float64 -> Float64 -> Float64

atan2 y x: the angle of the point (x, y), from -π to π.

>>> atan2 1.0 (-1.0)
2.356194490192345
>>> atan2 0.0 (-1.0)
3.141592653589793

exp

exp : Float64 -> Float64

e to the power x.

>>> exp 1.0
2.718281828459045

log

log : Float64 -> Float64

The natural logarithm; of 0, -Infinity; below 0, NaN.

>>> log e
1.0
>>> log 0.0
-Infinity

pow

pow : Float64 -> Float64 -> Float64

pow x y: x to the power y (as C’s pow: 1 to any power is 1, NaN too, and so is -1 to an infinite one).

>>> pow 2.0 10.0
1024.0
>>> pow 1.0 (0.0 /. 0.0)
1.0

abs

abs : Float64 -> Float64

x without its sign.

>>> abs (-2.5)
2.5

minFloat

minFloat : Float64 -> Float64 -> Float64

The lesser of two floats: the prelude’s min at Float64.

>>> minFloat 1.5 (-2.0)
-2.0

maxFloat

maxFloat : Float64 -> Float64 -> Float64

The greater of two floats: the prelude’s max at Float64.

>>> maxFloat 1.5 (-2.0)
1.5

isNaN

isNaN : Float64 -> Bool

Whether it is NaN, the one float not equal to itself.

>>> List.map isNaN [0.0 /. 0.0, 1.0 /. 0.0, 1.5]
Cons True (Cons False (Cons False Nil))

ceil

ceil : Float64 -> Int

The least whole number not below x, as the prelude’s floor is the greatest not above it; of what is not finite, 0.

>>> ceil 2.1
3
>>> ceil (-2.9)
-2
>>> List.map ceil [1.0 /. 0.0, -1.0 /. 0.0, 0.0 /. 0.0]
Cons 0 (Cons 0 (Cons 0 Nil))

round

round : Float64 -> Int

The whole number nearest x, a half away from 0 (as C’s round); of what is not finite, 0.

>>> round 2.5
3
>>> round (-2.5)
-3
>>> round 0.49999999999999994
0
>>> List.map round [1.0 /. 0.0, -1.0 /. 0.0, 0.0 /. 0.0]
Cons 0 (Cons 0 (Cons 0 Nil))

gcd

gcd : Int -> Int -> Int

The greatest integer that divides both, not below 0: of 0 and 0, 0.

>>> List.map (\p -> gcd (fst p) (snd p)) [MkPair 12 18, MkPair (-4) 6, MkPair 0 5, MkPair 0 0]
Cons 6 (Cons 2 (Cons 5 (Cons 0 Nil)))

lcm

lcm : Int -> Int -> Int

The least integer, not below 0, that both divide: of 0 and any, 0.

>>> List.map (\p -> lcm (fst p) (snd p)) [MkPair 4 6, MkPair (-3) 5, MkPair 0 7]
Cons 12 (Cons 15 (Cons 0 Nil))

tenTo

tenTo : Int -> Int

bitLength

bitLength : Int -> Int

How many bits the integer needs, without its sign: the place of the highest 1 of its size, from 1; of 0, 0 (Go’s big.Int.BitLen, as the fixed-width types’ bitLength).

>>> List.map bitLength [0, 1, 255, 256, -256, shiftL 1 100]
Cons 0 (Cons 1 (Cons 8 (Cons 9 (Cons 9 (Cons 101 Nil)))))

BinaryFormat

type BinaryFormat =
{ precision : Int, least : Int, infinity : Int, above : Int, under : Int }

binary64

binary64 : BinaryFormat

binary32

binary32 : BinaryFormat

bitsAt

bitsAt : BinaryFormat -> Int -> Int -> Int -> Int

nearestBits

nearestBits : BinaryFormat -> Int -> Int -> Int

fromDecimal

fromDecimal : Int -> Int -> Float64

m times 10 to the power e, correctly rounded: the float nearest it, a tie to the even, as C’s strtod reads a decimal — beyond the greatest float, an infinity, and below half the least, 0.0 (with the sign of m). Where m is below 2^53 and e within 22 of 0, it is one operation of exact floats; else it is worked out in integers.

>>> fromDecimal 1 (0 - 1)
0.1
>>> fromDecimal 5 (0 - 324)
5.0e-324
>>> fromDecimal 9007199254740993 0
9007199254740992.0
>>> fromDecimal (0 - 17976931348623159) 292
-Infinity