From 841dac234a78448b9b252d5640ba6c8efa19f81d Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 14 May 2018 18:07:38 -0500 Subject: [PATCH] fix(float): make `Float.{min,max}` compliant with revised IEEE754 closes #220 --- src/core/CCFloat.ml | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/core/CCFloat.ml b/src/core/CCFloat.ml index 0d6b53d0..93d90c4f 100644 --- a/src/core/CCFloat.ml +++ b/src/core/CCFloat.ml @@ -36,7 +36,7 @@ let max_finite_value = Pervasives.max_float let epsilon = Pervasives.epsilon_float -let is_nan x = (x : t) <> x +let is_nan x = Pervasives.classify_float x = Pervasives.FP_nan let add = (+.) let sub = (-.) @@ -47,16 +47,20 @@ let abs = Pervasives.abs_float let scale = ( *. ) let min (x : t) y = - if is_nan x || is_nan y then nan - else if x < y then x else y + match Pervasives.classify_float x, Pervasives.classify_float y with + | FP_nan, _ -> y + | _, FP_nan -> x + | _ -> if x < y then x else y let max (x : t) y = - if is_nan x || is_nan y then nan - else if x > y then x else y + match Pervasives.classify_float x, Pervasives.classify_float y with + | FP_nan, _ -> y + | _, FP_nan -> x + | _ -> if x > y then x else y let equal (a:float) b = a=b -let hash = Hashtbl.hash +let hash : t -> int = Hashtbl.hash let compare (a:float) b = Pervasives.compare a b type 'a printer = Format.formatter -> 'a -> unit