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:
> 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
again.
-- 
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