Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: malc <malc@pulsesoft.com>, caml-list@inria.fr
Subject: Re: How to read floats?
Date: Mon, 7 Aug 2000 18:06:41 +0200	[thread overview]
Message-ID: <20000807180641.07476@pauillac.inria.fr> (raw)
In-Reply-To: <Pine.LNX.4.21.0008020307340.929-100000@home.oyster.ru>; from malc on Wed, Aug 02, 2000 at 03:15:27AM +0400

> I need to read binary data from files, C floats (32bit) in particular,
> maybe someone here already got code to do that? 

If your file contains a large array of 32-bit floats, the "map_file"
functions from the Bigarray module could do the job.

Otherwise, you could read the C float in a character string of length
4, then call the following C function to convert it into a
floating-point number:

  #include <caml/mlvalues.h>
  #include <caml/alloc.h>

  value extract_float(value s)
  {
    union { float f; char c[4]; } buffer;
    memcpy(buffer.c, String_val(s), 4);
    return copy_double(buffer.d);
  }

and its Caml declaration:

  external extract_float : string -> float = "extract_float"

This assumes the float in the file have the same endianness as the
processor.  If not, you'll need to reverse the string somewhere.

Hope this helps,

- Xavier Leroy



  reply	other threads:[~2000-08-08 13:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-08-01 23:15 malc
2000-08-07 16:06 ` Xavier Leroy [this message]
2000-08-08  0:10   ` malc

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=20000807180641.07476@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=malc@pulsesoft.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