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 PAA07848; Thu, 2 May 2002 15:28:08 +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 PAA07844 for ; Thu, 2 May 2002 15:28:07 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from dr2chase.org (pool-141-154-31-59.bos.east.verizon.net [141.154.31.59]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g42DS6D25601 for ; Thu, 2 May 2002 15:28:06 +0200 (MET DST) Received: from Spooler by dr2chase.org (Mercury/32 v3.21c) ID MO00004D; 2 May 02 09:29:12 -0400 Received: from spooler by dr2chase.org (Mercury/32 v3.21c); 2 May 02 09:29:00 -0400 Received: from watergate.world.std.com (192.168.1.3) by dr2chase.org (Mercury/32 v3.21c) with ESMTP ID MG00004C; 2 May 02 09:28:54 -0400 Message-Id: <5.1.0.14.0.20020502085732.01f16fd0@pop.theWorld.com> X-Sender: chase@192.168.1.3 X-Mailer: QUALCOMM Windows Eudora Version 5.1 Date: Thu, 02 May 2002 09:28:51 -0400 To: caml-list@inria.fr From: David Chase Subject: Re: [Caml-list] string_of_float less accurate than sprintf "%f" ? In-Reply-To: <3CD134B0.8050100@ozemail.com.au> References: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk At 10:44 PM 5/2/2002 +1000, John Max Skaller wrote: >Beck01, Wolfgang wrote: > >>while doing some time measurements with Unix.gettimeofday() I >>discovered a problem with string_of_float: >> >># string_of_float 123456789.123456789;; >>- : string = "123456789.123" >There's another problem too: > ># string_of_float 42.0;; >- : string = "42" > >The result isn't an ocaml float literal. I'm new to Caml, but I did manage to slog through the sources enough to determine that Caml does it "wrong", at least as measured against what is possible and freely available. A version of the Java rules would be appropriate here -- as many digits as necessary to disambiguate from any other machine double/float, or at least one fractional digit, whichever is shorter. David Gay wrote some code to do this (gdtoa) that is available at netlib, and (having glued this code to a Java VM) I can provide some guidance in using it, if you would like. You can also use gdtoa to get other sorts of formatting -- n digits past the point, that sort of thing. Similarly, the transcendental functions available in fdlibm (Freely Distributable libm) are almost as fast as hardware (on those machines where there is hardware), but avoid the stupid input limits (e.g., |x| < 2**64) and also use a large enough value of PI to always deliver a result that is within 1 ulp, usually within 1/2 ulp (I have studied this problem more than I care to). The results will be fast, portable across all platforms, and as correct as you are likely to get, certainly as good as you are likely to get for free. Note: where available, hardware sqrt and log are usually usable, though I have not checked carefully in those portions of domain where their derivative is > 1. If someone decides to do this, please contact me, I have a list of fixes necessary for correct compilation of fdlibm under Microsoft's incorrect C compilers. Some ninny thought that the distributive law held true for machine FP, and they don't constant propagate "funny values" (NaN, -0) properly. yours, David Chase ------------------- 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