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: 2001-11-09 (13:32)
From: Thorsten Ohl <ohl@h...>
> 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.

Thanks.  I forgot to look among the Int* modules when I was hunting
for floating point conversions :-(.

> >     external float_of_bytes : string -> float = "float_of_bytes"
> >    b) on little endian machines one has to swap bytes.
> b) is not (big endian representation is enforced).

Now I'm confused.  On my Linux box, I have to swap bytes in order
to read binary floating point numbers from a stream (where I can not
mmap Bigarrays).

    let unsafe_rev8 s =
      let swap i1 i2 =
        let tmp = String.unsafe_get s i1 in
        String.unsafe_set s i1 (String.unsafe_get s i2);
        String.unsafe_set s i2 tmp in
      swap 0 7;
      swap 1 6;
      swap 2 5;
      swap 3 4

    let little_endian = true

    let input_binary_float ic =
      let buf = String.create 8 in
      really_input ic buf 0 8;
      if little_endian then
        unsafe_rev8 buf;
      float_of_bytes buf

But that's academical, because I will switch to Int64 now.  Thanks
Thorsten Ohl, Physics Department, TU Darmstadt -- [<=== PGP public key here]
Bug reports:  FAQ:
To unsubscribe, mail  Archives: