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 -> Float64floatCos
floatCos : Float64 -> Float64floatTan
floatTan : Float64 -> Float64floatAsin
floatAsin : Float64 -> Float64floatAcos
floatAcos : Float64 -> Float64floatAtan
floatAtan : Float64 -> Float64floatAtan2
floatAtan2 : Float64 -> Float64 -> Float64floatExp
floatExp : Float64 -> Float64floatLog
floatLog : Float64 -> Float64floatPow
floatPow : Float64 -> Float64 -> Float64pi
pi : Float64π, a circle’s circumference over its diameter.
e
e : Float64e, the base of the natural logarithm.
sin
sin : Float64 -> Float64The sine of an angle in radians.
>>> sin 0.00.0cos
cos : Float64 -> Float64The cosine of an angle in radians.
>>> cos pi-1.0tan
tan : Float64 -> Float64The tangent of an angle in radians.
>>> tan 0.00.0asin
asin : Float64 -> Float64The angle whose sine is x, from -π/2 to π/2.
>>> asin 1.01.5707963267948966acos
acos : Float64 -> Float64The angle whose cosine is x, from 0 to π.
>>> acos (-1.0)3.141592653589793atan
atan : Float64 -> Float64The angle whose tangent is x, from -π/2 to π/2.
>>> atan 1.00.7853981633974483atan2
atan2 : Float64 -> Float64 -> Float64atan2 y x: the angle of the point (x, y), from -π to π.
>>> atan2 1.0 (-1.0)2.356194490192345>>> atan2 0.0 (-1.0)3.141592653589793exp
exp : Float64 -> Float64e to the power x.
>>> exp 1.02.718281828459045log
log : Float64 -> Float64The natural logarithm; of 0, -Infinity; below 0, NaN.
>>> log e1.0>>> log 0.0-Infinitypow
pow : Float64 -> Float64 -> Float64pow 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.01024.0>>> pow 1.0 (0.0 /. 0.0)1.0abs
abs : Float64 -> Float64x without its sign.
>>> abs (-2.5)2.5minFloat
minFloat : Float64 -> Float64 -> Float64The lesser of two floats: the prelude’s min at Float64.
>>> minFloat 1.5 (-2.0)-2.0maxFloat
maxFloat : Float64 -> Float64 -> Float64The greater of two floats: the prelude’s max at Float64.
>>> maxFloat 1.5 (-2.0)1.5isNaN
isNaN : Float64 -> BoolWhether 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 -> IntThe 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.13>>> 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 -> IntThe whole number nearest x, a half away from 0 (as C’s round);
of what is not finite, 0.
>>> round 2.53>>> round (-2.5)-3>>> round 0.499999999999999940>>> 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 -> IntThe 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 -> IntThe 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 -> IntbitLength
bitLength : Int -> IntHow 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 : BinaryFormatbinary32
binary32 : BinaryFormatbitsAt
bitsAt : BinaryFormat -> Int -> Int -> Int -> IntnearestBits
nearestBits : BinaryFormat -> Int -> Int -> IntfromDecimal
fromDecimal : Int -> Int -> Float64m 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 09007199254740992.0>>> fromDecimal (0 - 17976931348623159) 292-Infinity