caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <xleroy@pauillac.inria.fr>
To: Thorsten Ohl <ohl@hep.tu-darmstadt.de>
Cc: Caml List <caml-list@pauillac.inria.fr>,
	"Rafael 'Dido' Sevilla" <sevillar@team.ph.inter.net>
Subject: Re: [Caml-list] how to split up a Caml float into its component bytes
Date: Fri, 9 Nov 2001 13:18:41 +0100	[thread overview]
Message-ID: <20011109131841.A5769@pauillac.inria.fr> (raw)
In-Reply-To: <15339.47220.792529.380015@heplix4.ikp.physik.tu-darmstadt.de>; from ohl@hep.tu-darmstadt.de on Fri, Nov 09, 2001 at 12:05:24PM +0100

> > I'm wondering how I would convert a floating point
> > number in OCaml (which I hope I am safe in assuming is IEEE-754) into
> > its equivalent bytes.
> 
> AFAIK, you have to write a C function.

Starting with OCaml 3.01, the Int64 module provides two functions
"bits_of_float" and "float_of_bits" which convert between floats and
an int64 matching the underlying 64-bit representation of the float.
>From the int64, extracting individual bytes is a simple matter of
shifting and masking.

> There is an undocumented primitive 
> 
>     external float_of_bytes : string -> float = "float_of_bytes"
> 
> defined in byterun/floats.c.  Unfortunately:
> 
>    a) the inverse is absent
>    b) on little endian machines one has to swap bytes.

a) is correct, b) is not (big endian representation is enforced).
Still, the functions from Int64 are less error-prone,
e.g. w.r.t. endianness.

- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


  reply	other threads:[~2001-11-09 13:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-09  3:29 Rafael 'Dido' Sevilla
2001-11-09 11:05 ` Thorsten Ohl
2001-11-09 12:18   ` Xavier Leroy [this message]
2001-11-09 13:32     ` Thorsten Ohl
2001-11-09 11:28 ` [Caml-list] how to split up a Caml float into its component bytes malc
2001-11-09 17:09 ` Ken Rose

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=20011109131841.A5769@pauillac.inria.fr \
    --to=xleroy@pauillac.inria.fr \
    --cc=caml-list@pauillac.inria.fr \
    --cc=ohl@hep.tu-darmstadt.de \
    --cc=sevillar@team.ph.inter.net \
    /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).