Skip to content
Menu
Contents

Reading the tags

pure

Same arguments, same result; touches nothing outside.

pure if arg2 is

Pure when that argument is a pure function; otherwise as impure as it is.

impure

Reads or changes something outside the program.

Float

1 type25 functions

64-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.

Float.round(Float.sqrt(2.0) * 100.0)
Returns
141

At a glance

  • Result

    Shorthand for [Ok(a) | Err(e)], the variant this module's fallible functions return.

  • 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 ==.

  • ne

    true when the two differ, so NaN differs from itself.

  • lt

    true when the first is less than the second, behind <.

  • le

    true when the first is at most the second, behind <=.

  • gt

    true when the first is greater than the second, behind >.

  • ge

    true when the first is at least the second, behind >=.

  • 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.

  • ceil

    Rounded up to an integral value, then converted.

  • round

    Halfway cases round to even.

  • truncate

    Rounded toward zero to an integral value, then converted.

  • toString

    A sign, the integer part, a . and up to 15 fractional digits with trailing zeros dropped, keeping at least one.

  • 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.

  • 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.

  • 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.

  • fromBits

    The float whose IEEE 754 bit pattern is the Int, so fromBits(toBits(x)) is x, bit for bit.

Types

Result

Result(a, e) = [Ok(a) | Err(e)]

Shorthand for [Ok(a) | Err(e)], the variant this module's fallible functions return.

Functions

add

(Float, Float) -> Floatpure

Addition, behind +, following IEEE 754.

sub

(Float, Float) -> Floatpure

Subtraction, behind -, following IEEE 754.

mul

(Float, Float) -> Floatpure

Multiplication, behind *, following IEEE 754.

div

(Float, Float) -> Floatpure

Division, behind /, following IEEE 754: dividing by zero is an infinity or NaN rather than a trap.

rem

(Float, Float) -> Floatpure

a - truncate(a / b) * b: the result takes the sign of the dividend.

eq

(Float, Float) -> Boolpure

true when the two are equal, behind ==. NaN equals nothing, itself included.

ne

(Float, Float) -> Boolpure

true when the two differ, so NaN differs from itself. a != b is eq negated, not a call to ne.

lt

(Float, Float) -> Boolpure

true when the first is less than the second, behind <. false when either is NaN.

le

(Float, Float) -> Boolpure

true when the first is at most the second, behind <=. false when either is NaN.

gt

(Float, Float) -> Boolpure

true when the first is greater than the second, behind >. false when either is NaN.

ge

(Float, Float) -> Boolpure

true when the first is at least the second, behind >=. false when either is NaN.

abs

(Float) -> Floatpure

The float without its sign: abs(-0.0) is 0.0.

neg

(Float) -> Floatpure

The float with its sign flipped: neg(0.0) is -0.0.

min

(Float, Float) -> Floatpure

The smaller of the two, -0.0 below 0.0, and NaN when either is NaN.

max

(Float, Float) -> Floatpure

The larger of the two, 0.0 above -0.0, and NaN when either is NaN.

sqrt

(Float) -> Floatpure

The square root, and NaN for a negative number.

floor

(Float) -> Intpure

Rounded down to an integral value, then converted. A value outside the Int range, an infinity or NaN traps.

ceil

(Float) -> Intpure

Rounded up to an integral value, then converted. A value outside the Int range, an infinity or NaN traps.

round

(Float) -> Intpure

Halfway cases round to even.

truncate

(Float) -> Intpure

Rounded toward zero to an integral value, then converted. A value outside the Int range, an infinity or NaN traps.

toString

(Float) -> Stringpure

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

(String) -> Result(Float, [InvalidFloat | e])pure

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

(Float) -> Stringpure

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

(Float) -> Intpure

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

(Int) -> Floatpure

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.

Reading the tags

pure

Same arguments, same result; touches nothing outside.

pure if arg2 is

Pure when that argument is a pure function; otherwise as impure as it is.

impure

Reads or changes something outside the program.