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.1 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 discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 80655BC50 for ; Sun, 1 Oct 2006 18:36:46 +0200 (CEST) Received: from smtp3-g19.free.fr (smtp3-g19.free.fr [212.27.42.29]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id k91GakTc020675 for ; Sun, 1 Oct 2006 18:36:46 +0200 Received: from [192.168.1.2] (che78-2-82-237-71-191.fbx.proxad.net [82.237.71.191]) by smtp3-g19.free.fr (Postfix) with ESMTP id DA572497D3; Sun, 1 Oct 2006 18:36:45 +0200 (CEST) Message-ID: <451FEE9C.9040501@inria.fr> Date: Sun, 01 Oct 2006 18:36:44 +0200 From: Xavier Leroy User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Sean McLaughlin Cc: caml-list@inria.fr Subject: Re: [Caml-list] float rounding References: <89957A27-4B6A-4FCF-A425-1468ECFA8B62@cmu.edu> In-Reply-To: <89957A27-4B6A-4FCF-A425-1468ECFA8B62@cmu.edu> X-Enigmail-Version: 0.91.0.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 451FEE9E.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; rounding:01 ocaml:01 ocaml:01 parser:01 compiler:01 lexer:01 parser:01 compilers:01 binary:01 bytecode:01 compiler:01 ocamlc:01 bytecode:01 gerd:01 stolpmann:01 > I'm using Ocaml for an interval arithmetic application. I"m curious > about what the Ocaml parser/compiler does to float constants. The lexer, parser and most of the compilers actually keep them as strings, just like they were given in the source code. Conversion to IEEE binary representation occurs: - For the bytecode compiler ocamlc, when the bytecode is generated. The conversion is performed by the float_of_string function, which is a thin wrapper around the C library strtod() function, as Gerd Stolpmann said. - For the native-code compiler ocamlopt, some ports go the float_of_string route, but most output the literal as a string in the generated assembly code, letting the assembler do the conversion. The assembler is likely to use strtod() or atof(), though. > 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? As Gerd said, the C standards do not guarantee this, so it really depends on how well-implemented your C library is. - Xavier Leroy