caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
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:06:19 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.64.0806222059280.3616@localhost> (raw)
In-Reply-To: <F2ECF8A0-AC98-4AC1-90C5-40AA94511004@erratique.ch>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed, Size: 1286 bytes --]



On Sun, 22 Jun 2008, Daniel Bünzli wrote:

> Richard gave you the reason.
>
> If you can serialize to binary, you can acheive what you want by serializing 
> the 64 bits integers you get with Int64.bits_of_float and applying 
> Int64.float_of_bits to the integers you deserialize.

Actually, for serialization, I strongly reccommend first using 
classify_float to split off and handle NaNs, Infinities, etc., then using 
frexp to split the float into a fraction and exponent.  The exponent is 
just an int, and the fractional part can be multiplied by, say, 2^56 and 
then converted into an integer.

The advantage of doing things this way, despite it being slightly more 
complicated, is two fold: one, it gaurentees you the ability to recovery 
the exact binary value of the float, and two, it sidesteps a huge number 
of compatibility issues between architectures- IIRC, IEEE 754 specifies 
how many bits have to be used to represent each part of the float, but not 
where they have to be in the word.  Also, if you use hexadecimal for 
saving the integers, this can actually be faster than converting to 
base-10, as conversion to base-10 isn't cheap.  It's a couple of more 
branches, but a lot of divs and mods get turned into shifts and ands.

Brian

  parent reply	other threads:[~2008-06-23  0:30 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
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 [this message]
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.0806222059280.3616@localhost \
    --to=bhurt@spnz.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=daniel.buenzli@erratique.ch \
    /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).