* [Caml-list] read a float(double) in a file in a binary file
@ 2002-08-01 11:05 polux moon
2002-08-02 16:58 ` Yaron M. Minsky
2002-08-02 17:06 ` Thorsten Ohl
0 siblings, 2 replies; 4+ messages in thread
From: polux moon @ 2002-08-01 11:05 UTC (permalink / raw)
To: caml-list
Hello
How can I read a float (double) in a binary format (output of a programe in
c ) from a file ?
I ve done it for it (read four byte and do some arithmetic) but for float i
don t know how to do
Thanks
_________________________________________________________________
Rejoignez MSN Hotmail, le plus important service de messagerie
http://www.hotmail.com/fr
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] read a float(double) in a file in a binary file
2002-08-01 11:05 [Caml-list] read a float(double) in a file in a binary file polux moon
@ 2002-08-02 16:58 ` Yaron M. Minsky
2002-08-02 17:06 ` Thorsten Ohl
1 sibling, 0 replies; 4+ messages in thread
From: Yaron M. Minsky @ 2002-08-02 16:58 UTC (permalink / raw)
To: polux moon; +Cc: caml-list
Use the Int64 function bits_of_float to get the int64 representation of
the float. Then using some arithmetic, extract the bytes and write them
out in the order of your choosing. To read, you do the same thing in
the opposite direction.
y
polux moon wrote:
>
> Hello
>
> How can I read a float (double) in a binary format (output of a programe
> in c ) from a file ?
> I ve done it for it (read four byte and do some arithmetic) but for
> float i don t know how to do
>
> Thanks
>
>
> _________________________________________________________________
> Rejoignez MSN Hotmail, le plus important service de messagerie
> http://www.hotmail.com/fr
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives:
> http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
> http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Caml-list] read a float(double) in a file in a binary file
2002-08-01 11:05 [Caml-list] read a float(double) in a file in a binary file polux moon
2002-08-02 16:58 ` Yaron M. Minsky
@ 2002-08-02 17:06 ` Thorsten Ohl
2002-08-05 9:29 ` Xavier Leroy
1 sibling, 1 reply; 4+ messages in thread
From: Thorsten Ohl @ 2002-08-02 17:06 UTC (permalink / raw)
To: polux moon; +Cc: caml-list
polux moon <polux_moon@hotmail.com> writes:
> How can I read a float (double) in a binary format (output of a
> programe in c ) from a file ?
Here's what I use:
(* This is hard coded value for Linux/Intel: *)
let little_endian = true
external float_of_bytes : string -> float = "float_of_bytes"
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 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
let input_binary_floats ic array =
let n = Array.length array in
let bytes = 8 * n in
let buf = String.create bytes in
really_input ic buf 0 bytes;
for i = 0 to n - 1 do
let s = String.sub buf (8 * i) 8 in
if little_endian then
unsafe_rev8 s;
array.(i) <- float_of_bytes s
done
--
Thorsten Ohl, Physics Dept., Wuerzburg Univ. -- ohl@physik.uni-wuerzburg.de
http://theorie.physik.uni-wuerzburg.de/~ohl/ [<=== PGP public key here]
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] read a float(double) in a file in a binary file
2002-08-02 17:06 ` Thorsten Ohl
@ 2002-08-05 9:29 ` Xavier Leroy
0 siblings, 0 replies; 4+ messages in thread
From: Xavier Leroy @ 2002-08-05 9:29 UTC (permalink / raw)
To: Thorsten Ohl; +Cc: polux moon, caml-list
> > How can I read a float (double) in a binary format (output of a
> > programe in c ) from a file ?
Here is a sample implementation, without any unsafe operation.
You didn't say whether your numbers are in big or little endian
representation, so the code handles both.
Hope this helps,
- Xavier Leroy
let output_float_big_endian oc f =
let n = ref (Int64.bits_of_float f) in
for i = 0 to 7 do
output_byte oc (Int64.to_int (Int64.shift_right_logical !n 56));
n := Int64.shift_left !n 8
done
let output_float_little_endian oc f =
let n = ref (Int64.bits_of_float f) in
for i = 0 to 7 do
output_byte oc (Int64.to_int !n);
n := Int64.shift_right_logical !n 8
done
let input_float_big_endian oc =
let n = ref Int64.zero in
for i = 0 to 7 do
let b = input_byte oc in
n := Int64.logor (Int64.shift_left !n 8) (Int64.of_int b)
done;
Int64.float_of_bits !n
let input_float_little_endian oc =
let n = ref Int64.zero in
for i = 0 to 7 do
let b = input_byte oc in
n := Int64.logor !n (Int64.shift_left (Int64.of_int b) (i*8))
done;
Int64.float_of_bits !n
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2002-08-05 9:30 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-01 11:05 [Caml-list] read a float(double) in a file in a binary file polux moon
2002-08-02 16:58 ` Yaron M. Minsky
2002-08-02 17:06 ` Thorsten Ohl
2002-08-05 9:29 ` Xavier Leroy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox