Float
1 type25 functions64-bit IEEE 754 floating-point numbers. The arithmetic and comparison
operators on Float values call the functions here, so a + b is
Float.add(a, b). There is no implicit conversion from Int: use
Int.toFloat, and floor, ceil,
round or truncate back.
Types
Result
Shorthand for [Ok(a) | Err(e)], the variant this module's fallible
functions return.
Functions
add
Addition, behind +, following IEEE 754.
sub
Subtraction, behind -, following IEEE 754.
mul
Multiplication, behind *, following IEEE 754.
div
Division, behind /, following IEEE 754: dividing by zero is an infinity
or NaN rather than a trap.
rem
a - truncate(a / b) * b: the result takes the sign of the dividend.
eq
true when the two are equal, behind ==. NaN equals nothing, itself included.
ne
true when the two differ, so NaN differs from itself. a != b is eq
negated, not a call to ne.
lt
true when the first is less than the second, behind <. false when either is NaN.
le
true when the first is at most the second, behind <=. false when either is NaN.
gt
true when the first is greater than the second, behind >. false when either is NaN.
ge
true when the first is at least the second, behind >=. false when either is NaN.
abs
The float without its sign: abs(-0.0) is 0.0.
neg
The float with its sign flipped: neg(0.0) is -0.0.
min
The smaller of the two, -0.0 below 0.0, and NaN when either is NaN.
max
The larger of the two, 0.0 above -0.0, and NaN when either is NaN.
sqrt
The square root, and NaN for a negative number.
floor
Rounded down to an integral value, then converted. A value outside the
Int range, an infinity or NaN traps.
ceil
Rounded up to an integral value, then converted. A value outside the Int
range, an infinity or NaN traps.
round
Halfway cases round to even.
truncate
Rounded toward zero to an integral value, then converted. A value outside the
Int range, an infinity or NaN traps.
toString
A sign, the integer part, a . and up to 15 fractional digits with trailing
zeros dropped, keeping at least one. NaN, Infinity and -Infinity print
as those words. Readable rather than shortest round-trip.
The integer part is written out exactly, however large, and never in an
exponent form, which Bert's float literals do not have either: the largest
float prints all 309 of its integer digits, then .0.
fromString
A number in JSON's grammar, and nothing else: an optional -, then 0 or
a non-zero digit and more digits, then optionally . and at least one
digit, then optionally e or E, an optional sign and at least one digit.
No + in front, no leading zeros, no whitespace, and no NaN or
Infinity.
The value is the float nearest the decimal the string spells, however many
digits it has, with ties to even. A value past the largest float is an
infinity of its sign, and one too small for the smallest subnormal is a
zero of its sign, so -0 is -0.0.
toShortestString
The fewest significant digits that fromString reads back as exactly this
float, choosing the nearer when two that short do, laid out as JavaScript
prints a number: 0.1, 100, 1e+21, 1.5e-7. Every zero prints 0,
and NaN, Infinity and -Infinity print as those words.
toBits
The IEEE 754 bit pattern of a float, read as an Int: the float's sign bit
is the Int's, so a negative float, -0.0 included, has a negative
pattern. No float traps.
fromBits
The float whose IEEE 754 bit pattern is the Int, so
fromBits(toBits(x)) is x, bit for bit. Every Int is some float: a
pattern with every exponent bit set and a non-zero fraction is a NaN.