Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] how to split up a Caml float into its component bytes
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Thorsten Ohl <ohl@h...>
Subject: [Caml-list] how to split up a Caml float into its component bytes
Rafael 'Dido' Sevilla writes:

> 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.  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 pair of primitives

    val unformatted_float_of_string : string -> float
    val unformatted_float_to_string : float -> string

together with

    val output_binary_float : out_channel -> float -> unit
    val input_binary_float : in_channel -> float

would be very helpful.  I made a version of input_binary_float from
input_string and some byte-swapping, but a portable version (that gives
me the warm fuzzy feeling that it does not run only on Linux) would be
very welcome: there are programs that spend most of their time parsing
and formatting floats.
-- 
Thorsten Ohl, Physics Department, TU Darmstadt -- ohl@hep.tu-darmstadt.de
http://heplix.ikp.physik.tu-darmstadt.de/~ohl/ [<=== PGP public key here]
-------------------
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