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 OAA15692; Wed, 4 Sep 2002 14:55:27 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 OAA15808 for ; Wed, 4 Sep 2002 14:55:26 +0200 (MET DST) Received: from tkb.mpl.com ([66.109.164.210]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g84CtO111813 for ; Wed, 4 Sep 2002 14:55:25 +0200 (MET DST) Received: from tkb.mpl.com ([66.109.164.210]) by tkb.mpl.com (8.12.3/8.12.3) with ESMTP id g84CtVmH044732; Wed, 4 Sep 2002 08:55:31 -0400 (EDT) (envelope-from tkb@tkb.mpl.com) From: "T. Kurt Bond" MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <15734.707.339302.596392@tkb.mpl.com> Date: Wed, 4 Sep 2002 08:55:31 -0400 To: Vincent Barichard CC: caml-list@pauillac.inria.fr Subject: RE: [Caml-list] float number In-Reply-To: References: X-Mailer: VM 7.04 under Emacs 21.2.1 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Vincent Barichard writes: > Hi, > > I've observed a strange behaviour of ocaml : > > let x = asin (-0.587527525714) in (x,x = (-0.628));; > - : float * bool = (-0.628, false) > > Why x doesn't equal to -0.628 ?? > > Thanks Because x isn't equal to -0.628, even though it prints as -0.628. The output routine doesn't print the value of x accurately. The same thing happens in C. O'Caml uses the C routine sprintf to format floating point numbers (see the function format_float in byterun/floats.c), and inherits poor float-printing routines from the C runtime library.. Asking CMU Common Lisp (with *read-default-float-format* set to DOUBLE-FLOAT, since O'Caml represents floating point numbers as doubles) for the value of (asin -0.587527525714) gives -0.6280000000001337, as does MzScheme. Here's a short (and non-portable) program that shows the problem occuring in C: #include #include int main (int argc, char **argv) { double x = asin (-0.587527525714); double y = -0.628; int *xp = &x; int *yp = &y; printf ("x: %g, x == (-0.628): %d\n", x, x == (-0.628)); printf ("*xp: %x *(xp+1): %x\n", *xp, *(xp+1)); printf ("*yp: %x *(yp+1): %x\n", *yp, *(yp+1)); exit (1); } On FreeBSD 4.6 running on a Pentium II I compiled it like this: cc -O -pipe x.c -lm -o x and the output was: x: -0.628, x == (-0.628): 0 *xp: 74bc6f33 *(xp+1): bfe41893 *yp: 74bc6a7f *(yp+1): bfe41893 Notice that *xp and *yp are not equal. More information on print floating point numbers can be found in the paper "Printing Floating-Point Numbers Quickly and Accurately"; see http://citeseer.nj.nec.com/28233.html -- T. Kurt Bond, tkb@tkb.mpl.com ------------------- 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