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 VAA22029; Thu, 30 Sep 2004 21:25:45 +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 VAA23670 for ; Thu, 30 Sep 2004 21:25:43 +0200 (MET DST) Received: from mproxy.gmail.com (rproxy.gmail.com [64.233.170.197]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id i8UJPh9L012130 for ; Thu, 30 Sep 2004 21:25:43 +0200 Received: by mproxy.gmail.com with SMTP id 74so229386rnk for ; Thu, 30 Sep 2004 12:25:42 -0700 (PDT) Received: by 10.38.181.36 with SMTP id d36mr1600917rnf; Thu, 30 Sep 2004 12:25:42 -0700 (PDT) Received: by 10.38.59.55 with HTTP; Thu, 30 Sep 2004 12:25:42 -0700 (PDT) Message-ID: <6b8a914204093012256481d02d@mail.gmail.com> Date: Thu, 30 Sep 2004 21:25:42 +0200 From: Remi Vanicat Reply-To: Remi Vanicat To: caml-list@inria.fr Subject: Re: [Caml-list] Printing floating point numbers & precision In-Reply-To: <415C39B0.2020909@research.att.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <415C39B0.2020909@research.att.com> X-Miltered: at concorde with ID 415C5DB7.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 2004:99 0400,:01 mff:99 mff:99 usr:01 3.07:01 printf:01 sprintf:01 printf:01 sprintf:01 faq:01 bin:01 ocaml:01 ocaml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Thu, 30 Sep 2004 12:52:00 -0400, mff wrote: > > Greetings, > > I have a question regarding the printing of floating point numbers. > Here's an example: > > lt-mff: /usr/bin/ocaml > Objective Caml version 3.07+2 > # max_float;; > - : float = 1.79769313486231571e+30 > > # Printf.sprintf "%e" max_float;; > - : string = "1.797693e+308" > > So using the default %e format to print a floating point number loses 11 digits > of precision. I could be more precise about the number of fractional digits: > > # Printf.sprintf "%.17e" max_float;; > - : string = "1.79769313486231571e+308" > > But this does not seem to work in general: > > # Printf.sprintf "%.17e" 12345.1;; > - : string = "1.23451000000000004e+04" > > I've combed the documentation to try to understand the relationship between > printing and precision (and accuracy!) but have come up empty handed. This is not a ocaml problem, this a problem with base 2 represention of decimal number : 1/10 is not representable exactly using standard ieee number, the more exact precicsion you could have is 0.100000004 (with may be some more 0 between the 1 and the 4). By the way it is a FAQ, and you could find dicsution about it int hte archive of this very mailing-list. ------------------- 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