Skip to content

Inaccurate Floating Point Literal Documentation #13

@davidfrogley

Description

@davidfrogley

Two separate issues.

  1. The overview for the floating-point types gives the following code snippet:
let x: float32 = 3.14;
let y: float64 = 2.718281828459045;
let z: float   = 1.0;  // defaults to float64

However, the code let x: float32 = 3.14; causes a compiler error because the literal 3.14 is of type float64, which cannot be assigned to a float32.
2. That same overview section mentions that "special values (Inf, -Inf, NaN) are natively supported" but does not give any indication of how to use such special values. One can simply generate them (e.g., 1.0 / 0.0, -1.0 / 0.0, or 0.0 / 0.0), but later in the comparison rules section we have this code snippet:

let n = Float.NaN;

n == n    // false  ← NaN is never equal to itself
n != n    // true
n <  1.0  // false
n >  1.0  // false

And also in the infinity propagates section we have this code snippet:

let inf = Float.Inf;
inf + 1.0     // → Inf
inf * -1.0    // → -Inf
inf - inf     // → NaN  ← be careful
inf * 0.0     // → NaN  ← be careful

Both of these seem to indicate that the constants Float.Inf and Float.NaN are defined somewhere, perhaps the Std library, but I was not able to find any such implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions