Skip to content

Negated float comparison is wrong for NaN: !(x < 0.0) compiles to x >= 0.0 #9026

Closed

Description

@cknitt

Negating a float comparison flips the operator, which is wrong for NaN:

let k = (x: float) => !(x < 0.0)

compiles to

function k(x) {
  return x >= 0.0;
}

k(NaN) returns false, but !(NaN < 0.0) is true.

The cause is Js_exp_make.not (compiler/core/js_exp_make.ml), which rewrites < to >=, >= to <, <= to > and > to <=. These are only complements when neither operand is NaN. The same function is used whenever the compiler negates a condition, e.g. when Js_stmt_make.if_ swaps the branches of an if with an empty then-branch, so other float comparisons can be affected too.

The rewrite is safe when the operands can't be NaN, e.g. for int, string and bigint comparisons. For floats, !(a < b) has to stay as it is.

Found by Codex while reviewing #9024.

Activity

  1. added theissue type on Oct 24, 2026
  2. added a commit that references this issue on Oct 24, 2026
    137367c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions