From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 5B7C0BBAF for ; Mon, 23 Jun 2008 11:06:51 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtgDAJQEX0jUnw7XcGdsb2JhbACCOZAyAQwFAgQHE50o X-IronPort-AV: E=Sophos;i="4.27,689,1204498800"; d="scan'208";a="14390941" Received: from fhw-relay07.plus.net ([212.159.14.215]) by mail3-smtp-sop.national.inria.fr with ESMTP; 23 Jun 2008 11:06:50 +0200 Received: from [80.229.56.224] (helo=beast.local) by fhw-relay07.plus.net with esmtp (Exim) id 1KAi0f-0005kh-Ls for caml-list@yquem.inria.fr; Mon, 23 Jun 2008 10:06:49 +0100 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Strange behaviour of string_of_float Date: Mon, 23 Jun 2008 09:35:40 +0100 User-Agent: KMail/1.9.9 References: <4b5157c30806220956p164d8346i3e832bc8b8666306@mail.gmail.com> <20080622195831.GA22384@annexia.org> In-Reply-To: <20080622195831.GA22384@annexia.org> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200806230935.40994.jon@ffconsultancy.com> X-Plusnet-Relay: d8e77c63b7987a5baf6e044ca0c9c317 X-Spam: no; 0.00; 0200,:01 ocaml's:01 unspecified:01 sprintf:01 printf:01 sprintf:01 ocaml's:01 bytecode:01 frog:98 wrote:01 wrote:01 caml-list:01 arithmetic:01 behaviour:01 finite:02 On Sunday 22 June 2008 20:58:31 Richard Jones wrote: > On Sun, Jun 22, 2008 at 06:56:22PM +0200, Paolo Donadeo wrote: > > string_of_float is not the inverse of float_of_string, at least in > > this example. > > Yes, you wouldn't expect it to be, because the string is an > approximate base 10 representation of the float... That is not true. All finite floats have exact finite decimal representations. So it is perfectly reasonable to expect the conversions to recover the original number exactly. As Paolo has shown, OCaml's current string_of_float function is approximate. The accuracy of this routine is unspecified but a quick test indicates that it is simply printing too few digits to be exact: # string_of_float pi;; - : string = "3.14159265359" Fortunately, you can ask sprintf to generate a sufficiently accurate result: # open Printf;; # sprintf "%0.17g" pi;; - : string = "3.1415926535897931" The float_of_string function does then recover the number exactly in this case: # float_of_string "3.1415926535897931" -. pi;; - : float = 0. Also, you should keep in mind in this context that calculations may be done with 80-bit float arithmetic in registers or truncated to 64-bits when stored to memory. Moreover, OCaml's bytecode and native code targets can behave differently in this context. I do not believe that is a problem with Paolo's code here though. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/products/?e