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 AAA17066; Tue, 10 Dec 2002 00:48:55 +0100 (MET) 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 AAA16773 for ; Tue, 10 Dec 2002 00:48:54 +0100 (MET) Received: from swan.mail.pas.earthlink.net (swan.mail.pas.earthlink.net [207.217.120.123]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id gB9Nmr110932 for ; Tue, 10 Dec 2002 00:48:54 +0100 (MET) Received: from user-0cev2t6.cable.mindspring.com ([24.239.139.166] helo=[192.168.0.3]) by swan.mail.pas.earthlink.net with esmtp (Exim 3.33 #1) id 18LXbG-0005tP-00; Mon, 09 Dec 2002 15:46:10 -0800 Subject: Re: [Caml-list] float pretty-printing precision, once more. From: "Yaron M. Minsky" To: jeanmarc.eber@lexifi.com Cc: caml-list@inria.fr In-Reply-To: <1039475060.3df52174c32a4@imp.pro.proxad.net> References: <1039475060.3df52174c32a4@imp.pro.proxad.net> Content-Type: text/plain Organization: Cornell University Message-Id: <1039477568.16258.1308.camel@dragonfly.localdomain> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.0 Date: 09 Dec 2002 18:46:08 -0500 Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk I'm no expert on floating point representations either, but sprintf with a sufficiently long length seems to work. # let test x = float_of_string (sprintf "%.30e" x) = x;; val test : float -> bool = # test (sqrt 2.);; - : bool = true # test (sqrt 2. *. 1.343e26);; - : bool = true y On Mon, 2002-12-09 at 18:04, jeanmarc.eber@lexifi.com wrote: > caml 3.06+1: > > # let f = 1. /. 86400.;; > val f : float = 1.15740740741e-05 > # let s = string_of_float f;; > val s : string = "1.15740740741e-05" > # let f1 = float_of_string s;; > val f1 : float = 1.15740740741e-05 > # f1 = f;; > - : bool = false > # f1 -. f;; > - : float = 2.59259844496e-17 > > This situation may be understandable, but is unfortunate. > > Disclaimer: I'm not a specialist of the IEEE float format. > > Do I have at hand, at least on an architecture supporting the IEEE format, a > function that pretty-prints any valid float value (by valid I mean that I > exclude the "special" values like NaN, infinity, etc.) so that > float_of_string applied to the resulting string returns my initial value, > or, at least, a value that, if substracted from my initial one, returns > zero ? > > Background: > > In fact, my question goes a little bit further, as it concerns indeed the > parsing of floats in the caml compiler (that uses internally float_of_string > if I'm correct). > > Suppose you calculate somewhere (with an caml program, say) a float > constant (such a calculation may last for hours!), and you want after > obtaining the result to *generate* a caml source using this calculated > value. You will probably generate something like > > let my_const = > > But my example shows that you are loosing precision and accuracy if you > just use string_of_float. > > Of course the goal is to incorporate this value in a caml source, not > to read it in binary form from a file (that would be easy!). > > Do anybody know a solution to my problem ? > > Jean-Marc Eber > > ------------------- > 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 ------------------- 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