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=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id A2B19BC50 for ; Tue, 3 Oct 2006 23:05:33 +0200 (CEST) Received: from mallaury.nerim.net (smtp-102-tuesday.noc.nerim.net [62.4.17.102]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k93L5XVj005872 for ; Tue, 3 Oct 2006 23:05:33 +0200 Received: from karryall.dnsalias.org (karryall.dnsalias.org [213.41.240.180]) by mallaury.nerim.net (Postfix) with ESMTP id E858D4F3EE; Tue, 3 Oct 2006 23:05:19 +0200 (CEST) Received: by karryall.dnsalias.org (Postfix, from userid 500) id B5EA092DB65; Tue, 3 Oct 2006 23:05:32 +0200 (CEST) From: Olivier Andrieu MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Message-ID: <17698.53404.408141.945571@karryall.dnsalias.org> Date: Tue, 3 Oct 2006 23:05:32 +0200 To: Christophe Raffalli Cc: Sean McLaughlin , caml-list@inria.fr Subject: Re: [Caml-list] float rounding In-Reply-To: <4522C131.9010400@univ-savoie.fr> References: <89957A27-4B6A-4FCF-A425-1468ECFA8B62@cmu.edu> <4522C131.9010400@univ-savoie.fr> X-Mailer: VM 7.19 under Emacs 21.4.1 Content-Transfer-Encoding: quoted-printable X-Miltered: at concorde with ID 4522D09D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; andrieu:01 oandrieu:01 rounding:01 christophe:01 raffalli:01 ocaml:01 ocaml:01 parser:01 compiler:01 compiler:01 printf:01 scanf:01 printf:01 val:01 2006:98 Christophe Raffalli [Tuesday 3 October 2006] : > > Sean McLaughlin a =E9crit : > > Hello, > > > > I'm using Ocaml for an interval arithmetic application. I"m > > curious about what the Ocaml parser/compiler does to float > > constants. May I assume that for any constant I enter, > > eg. 3.1415... (for N digits of pi), that the compiler will give > > me a closest machine representable number? i.e., if I bound a > > constant by the previous and next floating point value to that > > given me by the compiler, will it always be the case that my > > original (mathematical) constant lies in that interval? > > >=20 > By the way, float constants need to be written in hexadecimal and > this is missing to the printf/scanf functions (it is what man > printf says at least) ... just compute how many decimals you need > to write the exact value of 2^{-n} as a decimal float constant (0,5 > 0,25 0,125 0,625e-1 0,3125e-1 ...). float_of_string (which uses strtod) already knows how to read a hexadecimal float and there's a hack to have the C printf do the writing: ,---- | # external format_float: string -> float -> string =3D "caml_format_flo= at" ;; | external format_float : string -> float -> string =3D "caml_format_floa= t" | # let hex_float =3D format_float "%a" ;; | val hex_float : float -> string =3D | # hex_float 1.25 ;; | - : string =3D "0x1.4p+0" | # float_of_string "0x1.4p+0" ;; | - : float =3D 1.25 | # hex_float 0.1 ;; | - : string =3D "0x1.999999999999ap-4" `---- --=20 Olivier