caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Olivier Andrieu" <oandrieu@gmail.com>
To: p.donadeo@gmail.com, "Mattias Engdegård" <mattias@virtutech.se>,
	caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Strange behaviour of string_of_float
Date: Mon, 23 Jun 2008 10:50:30 +0200	[thread overview]
Message-ID: <95513600806230150v6122959rb6b5e0770b237ab1@mail.gmail.com> (raw)
In-Reply-To: <20080623083254.7901F82040@kicki.hq.vtech>

On Mon, Jun 23, 2008 at 10:32, Mattias Engdegård <mattias@virtutech.se> wrote:
>>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 = "caml_format_float"
  let hex_string_of_float f =
    format_float "%a" f

# hex_string_of_float pi ;;
- : string = "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).

-- 
  Olivier


  reply	other threads:[~2008-06-23  8:50 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-22 16:56 Paolo Donadeo
2008-06-22 19:58 ` [Caml-list] " Richard Jones
2008-06-22 20:45   ` Paolo Donadeo
2008-06-23  1:25     ` Brian Hurt
2008-06-23  7:50       ` Paolo Donadeo
2008-06-23  8:32     ` Mattias Engdegård
2008-06-23  8:50       ` Olivier Andrieu [this message]
2008-06-23  8:35   ` Jon Harrop
2008-06-22 20:32 ` Daniel Bünzli
2008-06-22 20:50   ` Paolo Donadeo
2008-06-23  8:45     ` David Allsopp
2008-06-23  8:55       ` Olivier Andrieu
2008-06-23 12:06         ` David Allsopp
2008-06-23  1:06   ` Brian Hurt
2008-06-23  7:58     ` Xavier Leroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=95513600806230150v6122959rb6b5e0770b237ab1@mail.gmail.com \
    --to=oandrieu@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=mattias@virtutech.se \
    --cc=p.donadeo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).