From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA00106 for caml-red; Fri, 26 Jan 2001 22:10:41 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA07639 for ; Thu, 25 Jan 2001 17:05:19 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f0PG5HL18610; Thu, 25 Jan 2001 17:05:17 +0100 (MET) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA15311; Thu, 25 Jan 2001 17:05:17 +0100 (MET) Date: Thu, 25 Jan 2001 17:05:17 +0100 From: Xavier Leroy To: Olivier Andrieu Cc: caml-list@inria.fr Subject: Re: Floating-point classification Message-ID: <20010125170517.A26956@pauillac.inria.fr> References: <14956.7306.706689.299369@akasha.ijm.jussieu.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <14956.7306.706689.299369@akasha.ijm.jussieu.fr>; from andrieu@oxygene.ijm.jussieu.fr on Mon, Jan 22, 2001 at 12:42:02PM +0100 Sender: weis@pauillac.inria.fr > Is there a way to test whether a float has value 'nan' or 'inf' ? I > didn't find in the standard library the equivalent of libc functions > isfinite() or isnan() (those are macros I think). Right. Eventually, similar functions should be added to OCaml. Currently, OCaml provides all math functions specified in ANSI C (the lowest common denominator of all platforms it supports), but isfinite(), isnan() and fpclassify() are from ISO C9X, not ANSI C. > I think that in C you can test if a float is nan by comparing it > with itself. It seems to work in OCaml too : Yes, it works provided the argument to = is statically known to be of type "float" (so that the float-specific equality operation is performed instead of generic equality). As for infinities, you can also test equality with +inf or -inf. The following functions should do the job: let isnan (x: float) = x <> x let posinf = 1.0 /. 0.0 let neginf = -. posinf let isposinf x = x = posinf let isneginf x = x = neginf let isfinite x = not(isnan x || isposinf x || isneginf x) etc, etc. - Xavier Leroy