Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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

    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 -- [<=== PGP public key here]
Bug reports:  FAQ:
To unsubscribe, mail  Archives: