From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id FAA05769; Thu, 15 Jul 2004 05:51:44 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id FAA05390 for ; Thu, 15 Jul 2004 05:51:43 +0200 (MET DST) Received: from mproxy.gmail.com (mproxy.gmail.com [216.239.56.250]) by nez-perce.inria.fr (8.12.10/8.12.10) with SMTP id i6F3pfEV003062 for ; Thu, 15 Jul 2004 05:51:42 +0200 Received: by mproxy.gmail.com with SMTP id r62so571681cwc for ; Wed, 14 Jul 2004 20:51:41 -0700 (PDT) Received: by 10.11.117.27 with SMTP id p27mr424823cwc; Wed, 14 Jul 2004 20:51:41 -0700 (PDT) Message-ID: <891bd3390407142051653aae76@mail.gmail.com> Date: Wed, 14 Jul 2004 23:51:41 -0400 From: Yaron Minsky Reply-To: yminsky@cs.cornell.edu To: Caml Mailing List Subject: [Caml-list] min, max and nan Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 40F5FF4D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; yaron:01 minsky:01 yminsky:01 nans:01 doable:01 val:01 val:01 affairs:99 yaron:01 bool:01 ocaml:01 ocaml:01 int:01 int:01 polymorphic:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk One of the oddities associated with NaNs in ocaml is what happens when you take the min of something and nan. Witness: # min nan 3.;; - : float = 3. # min 3. nan;; - : float = nan Surprisingly, the result depends on the ordering. Why it does so is clear once you look at the implementation and ponder for a moment the IEEE requirements on comparisons involving nan. When using ocaml 3.07, I tried and failed to come up with a polymorphic min that behaved reasonably in this case, that is, that returned nan when either argument is nan. With 3.08, this is now doable. Here's how it works: # let contains_nan x = x <> x;; val contains_nan : 'a -> bool = # let nmin x y = if contains_nan x then x else if contains_nan y then y else min x y;; val nmin : 'a -> 'a -> 'a = # nmin 3. nan;; - : float = nan # nmin nan 3.;; - : float = nan Still, we don't quite escape from the oddities of nan. We still get order dependence in the case of larger data structures that include nan's: # nmin (1,nan) (2,nan);; - : int * float = (1, nan) # nmin (2,nan) (1,nan);; - : int * float = (2, nan) But in my mind, the current state of affairs is a big improvement. Yaron ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners