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.2 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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id E6BDFBBAF for ; Mon, 23 Jun 2008 10:50:30 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhABANQAX0jRVYC/d2dsb2JhbACSKEMBDAMEBAkRA5lAg1s X-IronPort-AV: E=Sophos;i="4.27,689,1204498800"; d="scan'208";a="12434314" Received: from fk-out-0910.google.com ([209.85.128.191]) by mail2-smtp-roc.national.inria.fr with ESMTP; 23 Jun 2008 10:50:30 +0200 Received: by fk-out-0910.google.com with SMTP id k31so2276491fkk.11 for ; Mon, 23 Jun 2008 01:50:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=pUajcVDV8Qy0X0p0XlGVpLZeOOiUF9DLYBGcLoUPTC4=; b=B9/GckoCO8H8QHU7LBrKpjDTS3isnpc8+h5e81HMkaYvKMhpe4JkBnbX9HDATEv4yY n8JcejDL1A7O9elInc1urqpN3poJYucFIWhyD7LBxdxl836Woo1hLok2mtFjFYSFH4EW odfoPahFA/XUd4i7PDwUw7sXFQERTPDUFRJIA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=MER4DDh0LC/DnHNJ5fZswYxR/1othvBEu4GnqHU+YmTcWtpacEw83m0w254CDzo24m ZRtg56xURXJkhrZz5z3j6LwGr3uZe7qJ0CIl0uGn0RXZuESKiGJYXATxIjMXu1pDIO+c SXE+qSC9TxihsgQx23+hfaWWO+WPAZ8u8ERDI= Received: by 10.78.134.7 with SMTP id h7mr2582253hud.94.1214211030344; Mon, 23 Jun 2008 01:50:30 -0700 (PDT) Received: by 10.78.40.8 with HTTP; Mon, 23 Jun 2008 01:50:30 -0700 (PDT) Message-ID: <95513600806230150v6122959rb6b5e0770b237ab1@mail.gmail.com> Date: Mon, 23 Jun 2008 10:50:30 +0200 From: "Olivier Andrieu" To: p.donadeo@gmail.com, "=?ISO-8859-1?Q?Mattias_Engdeg=E5rd?=" , caml-list@yquem.inria.fr Subject: Re: [Caml-list] Strange behaviour of string_of_float In-Reply-To: <20080623083254.7901F82040@kicki.hq.vtech> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <4b5157c30806220956p164d8346i3e832bc8b8666306@mail.gmail.com> <20080622195831.GA22384@annexia.org> <4b5157c30806221345p9be5daeqc65b3f6cfb3219b3@mail.gmail.com> <20080623083254.7901F82040@kicki.hq.vtech> X-Spam: no; 0.00; andrieu:01 oandrieu:01 mattias:01 mattias:01 ocaml:01 ocaml's:01 printf:01 pervasives:01 specifier:01 23,:98 interchange:98 wrote:01 readable:01 msvc:01 caml-list:01 On Mon, Jun 23, 2008 at 10:32, Mattias Engdeg=E5rd w= rote: >>My intent is to extract an ASCII representation of an OCaml float >>value so that it can be used to recreate *exactly* the same value, at >>least on the same architecture. > > A somewhat more portable (and readable, maybe) representation of > floating-point numbers is in hex (a la C99). It is independent of the > precision and binary format used. Unfortunately, ocaml's Printf has > already appropriated %a for a different purpose, but it remains a good > option for those willing to do some manual work. > > I have used it in the past to good effect in text-based interchange > formats between applications written in C. Indeed, that's a good solution. It's possible to use this %a conversion directly, without writing external C code: (* this external is in pervasives.ml *) external format_float : string -> float -> string =3D "caml_format_float" let hex_string_of_float f =3D format_float "%a" f # hex_string_of_float pi ;; - : string =3D "0x1.921fb54442d18p+1" Mind that this only works if the underlying C library knows how to handle this C99 conversion specifier (MSVC6 doesn't for instance). --=20 Olivier