While trying to figure out some unexpected behavior with NaNs, I've discovered the following:
Caml_obj uses Caml.int_compare to compare numbers, both ints and floats.
Caml.int_compare considers NaN > NaN and any other value. (which is of course fine if it's never used on NaNs, but it is)
Caml.float_compare considers NaN == NaN.
Caml.float_compare considers NaN less than any other value.
Caml.obj.lessthan and Caml_obj.greatherthan uses Caml_obj.compare, and therefore also considers NaN the greatest of all values! This is inconsistent with both OCaml and JavaScript behaviour. Comparing NaN to anything, in any way, should return false.
Simple repro:
// These will all print true
Js.log(compare(nan, nan) == 0) // Caml.float_compare
Js.log(compare(nan, 33.3) == -1) // Caml.float_compare
Js.log(compare(nan->Obj.magic, nan->Obj.magic) == 1) // Caml_obj.compare (which in turn uses Caml.int_compare on numbers)
Js.log(nan > nan == false) // inlined as >, correct behaviour
Js.log(nan->Obj.magic > nan->Obj.magic == true) // Canl_obj.greaterthan
While trying to figure out some unexpected behavior with
NaNs, I've discovered the following:Caml_objusesCaml.int_compareto compare numbers, both ints and floats.Caml.int_compareconsidersNaN > NaNand any other value. (which is of course fine if it's never used onNaNs, but it is)Caml.float_compareconsidersNaN == NaN.Caml.float_compareconsidersNaNless than any other value.Caml.obj.lessthanandCaml_obj.greatherthanusesCaml_obj.compare, and therefore also considersNaNthe greatest of all values! This is inconsistent with both OCaml and JavaScript behaviour. ComparingNaNto anything, in any way, should returnfalse.Simple repro: