From: Xavier Leroy <xavier.leroy@inria.fr>
To: Thorsten Ohl <ohl@physik.uni-wuerzburg.de>
Cc: polux moon <polux_moon@hotmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] read a float(double) in a file in a binary file
Date: Mon, 5 Aug 2002 11:29:56 +0200 [thread overview]
Message-ID: <20020805112956.B7554@pauillac.inria.fr> (raw)
In-Reply-To: <15690.48147.967042.16431@wptx47.physik.uni-wuerzburg.de>; from ohl@physik.uni-wuerzburg.de on Fri, Aug 02, 2002 at 07:06:27PM +0200
> > 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
prev parent reply other threads:[~2002-08-05 9:30 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-08-01 11:05 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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20020805112956.B7554@pauillac.inria.fr \
--to=xavier.leroy@inria.fr \
--cc=caml-list@inria.fr \
--cc=ohl@physik.uni-wuerzburg.de \
--cc=polux_moon@hotmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox