Deriving.eq and Float.nan

Perhaps I’m misunderstanding the question, but let me try:

compare x y = 0 is the standard way in OCaml to compare floats while considering NANs equal. From float.mli:

val compare: t -> t -> int
(** [compare x y] returns [0] if [x] is equal to [y], a negative integer if [x]
    is less than [y], and a positive integer if [x] is greater than
    [y]. [compare] treats [nan] as equal to itself and less than any other float
    value.  This treatment of [nan] ensures that [compare] defines a total
    ordering relation.  *)

Further, ppx_deriving, lets you specify custom equal functions on a per-field basis. From GitHub - ocaml-ppx/ppx_deriving: Type-driven code generation for OCaml

Putting the two together should result in a nice solution. The question is perhaps if ppx_deriving’s default equality should be changed, or its documentation should mention this edge-case.

3 Likes