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.9 required=5.0 tests=AWL,SPF_NEUTRAL 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 EE197BC0A; Fri, 24 Nov 2006 01:28:50 +0100 (CET) Received: from nef2.ens.fr (nef2.ens.fr [129.199.96.40]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id kAO0Smuu007784; Fri, 24 Nov 2006 01:28:50 +0100 Received: from di.ens.fr (di.ens.fr [129.199.99.1]) by nef2.ens.fr (8.13.6/1.01.28121999) with ESMTP id kAO0SjBY049409 ; Fri, 24 Nov 2006 01:28:45 +0100 (CET) X-Envelope-To: caml-list@inria.fr Received: from [192.168.138.143] (mna75-4-82-225-77-14.fbx.proxad.net [82.225.77.14]) (authenticated bits=0) by di.ens.fr (8.13.6/jb-1.1) id kANN0iQ2009902 ; Fri, 24 Nov 2006 00:00:45 +0100 Message-ID: <4566281A.9080402@ens.fr> Date: Fri, 24 Nov 2006 00:00:42 +0100 From: David Monniaux User-Agent: Thunderbird 1.5.0.8 (X11/20061107) MIME-Version: 1.0 To: Xavier Leroy Cc: seanmcl@cmu.edu, caml-list@inria.fr Subject: Re: [Caml-list] float rounding References: <89957A27-4B6A-4FCF-A425-1468ECFA8B62@cmu.edu> <451FEE9C.9040501@inria.fr> In-Reply-To: <451FEE9C.9040501@inria.fr> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-milter (http://amavis.org/) X-Greylist: Delayed for 01:27:59 by milter-greylist-2.1.9 (nef2.ens.fr [129.199.96.32]); Fri, 24 Nov 2006 01:28:45 +0100 (CET) X-Miltered: at discorde with ID 45663CC0.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; monniaux:01 monniaux:01 rounding:01 ocaml:01 ocaml:01 parser:01 compiler:01 native-code:01 compiler:01 ocamlopt:01 gerd:01 notation:01 printf:01 powerpc:01 biblio:01 Xavier Leroy a écrit : >> I'm using Ocaml for an interval arithmetic application. I"m curious >> about what the Ocaml parser/compiler does to float constants. >> >> - 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. > As Gerd said, the C standards do not guarantee this, so it really > depends on how well-implemented your C library is. In C, if you are really concerned about getting your exact constants up to the last bit, you should use the hexadecimal float notation introduced in C99. You should also use "%a" or "%A" to print out your floating-point values in hexadecimal if you intend to reload them on another IEEE-754 system in a portable way. (Unfortunately on OCaml, %a is already used for another use... Xavier, what do you think of adding the hexadecimal float conversion to the family of printf functions?) Anyway, "IEEE-754 compliant", as used by common vendors, is a fairly vague claim. For instance, contrary to a commonly held belief, the same C or Caml program ran on two different major platforms (say, IA32 vs AMD64 or IA32 vs PowerPC) can give different results, possibly even depending on compiler options, and this has nothing to do with how they parse constants... You might wish to read my preprint: http://www.di.ens.fr/~monniaux/biblio/pitfalls_of_floating_point.pdf The only widespread language that I know about that has fixed semantics for floating-point is Java if the 'strictfp' modifier is used. (Originally, Java was supposed to have a fully fixed semantics, but they realized that implementing it over the 80387 compatible floating-point stack was difficult and that it also precluded some optimizations, so they relaxed it by default and introduced 'strictfp'. Goodbye predictability.)