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 OAA01854; Tue, 10 Dec 2002 14:09:40 +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 OAA01806 for ; Tue, 10 Dec 2002 14:09:40 +0100 (MET) Received: from isis.lip6.fr (isis.lip6.fr [132.227.60.2]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id gBAD9d128758 for ; Tue, 10 Dec 2002 14:09:39 +0100 (MET) Received: from spi.lip6.fr (IDENT:root@spi.lip6.fr [132.227.83.55]) by isis.lip6.fr (8.12.4/jtpda-5.4+victor) with ESMTP id gBAD9c48032053 for ; Tue, 10 Dec 2002 14:09:38 +0100 X-pt: isis.lip6.fr Received: from inria.fr (macaque.lip6.fr [132.227.83.193]) by spi.lip6.fr (8.8.7/jtpda-5.2) with ESMTP id OAA22939 for ; Tue, 10 Dec 2002 14:09:38 +0100 Date: Tue, 10 Dec 2002 14:09:19 +0100 Subject: Re: [Caml-list] float pretty-printing precision, once more. Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v548) From: Damien Doligez To: caml-list@inria.fr Content-Transfer-Encoding: 7bit In-Reply-To: <1039475060.3df52174c32a4@imp.pro.proxad.net> Message-Id: <97FCFA94-0C40-11D7-B751-0003930FCE12@inria.fr> X-Mailer: Apple Mail (2.548) X-Scanned-By: isis.lip6.fr Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Tuesday, Dec 10, 2002, at 00:04 Europe/Paris, jeanmarc.eber@lexifi.com wrote: > caml 3.06+1: [string_of_float loses precision on floating-point numbers] In the current working version (3.06+18), the precision used by string_of_float has been increased to 17 digits. I *think* this is enough to represent all the double-precision floating-point numbers: Objective Caml version 3.06+18 (2002-11-07) # let f = 1. /. 86400.;; val f : float = 1.1574074074074073e-05 # let s = string_of_float f;; val s : string = "1.1574074074074073e-05" # let f1 = float_of_string s;; val f1 : float = 1.1574074074074073e-05 # f1 = f;; - : bool = true # f1 -. f;; - : float = 0. However, it has the unfortunate side-effect of revealing this awful truth about FP numbers: many "interesting" numbers are impossible to represent in floating-point. For example: # 0.1;; - : float = 0.10000000000000001 There is no floating-point number equal to 0.1 and the best approximation you can get is 0.10000000000000001. I expect this will quickly become a much-used FAQ entry... I don't know if we will keep this change for the next release of O'Caml. But you can always use (Printf.sprintf "%.17g") instead of string_of_float, and you'll get all the digits, significant or not. -- Damien ------------------- 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