From: Brian Hurt <bhurt@spnz.org>
To: Paolo Donadeo <p.donadeo@gmail.com>
Cc: caml-list caml-list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Strange behaviour of string_of_float
Date: Sun, 22 Jun 2008 21:25:15 -0400 (EDT) [thread overview]
Message-ID: <Pine.LNX.4.64.0806222108380.3616@localhost> (raw)
In-Reply-To: <4b5157c30806221345p9be5daeqc65b3f6cfb3219b3@mail.gmail.com>
On Sun, 22 Jun 2008, Paolo Donadeo wrote:
> I know what a float number is from my numerical analysis course :-).
>
> In any case, what is the suggested way to serialize/deserialize a
> float number in OCaml? The Sexplib, for example, suffers the same
> problem of the string_of_float function:.
>
> 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.
>
Code something like this should work:
let encode_float x =
match (classify_float x) with
| FP_zero -> if (x = -0.0) then "-0.0" else "0.0"
| FP_infinite -> if (x = neg_infinity) then "-INF" else "INF"
| FP_nan -> "NaN"
| _ ->
let s = x < 0.0 in
let x = abs_float x in
let frac, exp = frexp x in
let frac = frac *. 268435456.0 in (* 2^28 *)
let i1 = int_of_float frac in
let i2 = int_of_float ((frac -. (floor frac)) *. 268435456.0) in
let exp = exp - 56 in
let s2 = exp < 0 in
let exp = if exp < 0 then -exp else exp in
Printf.sprintf "%c%07X%07XX%c%X" (if s then '-' else '+') i1 i2
(if s2 then '-' else '+') exp
;;
I'll leave the decode to you- it should be obvious, once you discover the
ldexp function.
Brian
next prev parent reply other threads:[~2008-06-23 0:49 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 [this message]
2008-06-23 7:50 ` Paolo Donadeo
2008-06-23 8:32 ` Mattias Engdegård
2008-06-23 8:50 ` Olivier Andrieu
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=Pine.LNX.4.64.0806222108380.3616@localhost \
--to=bhurt@spnz.org \
--cc=caml-list@yquem.inria.fr \
--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).