Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: malc <malc@pulsesoft.com>
To: Xavier Leroy <Xavier.Leroy@inria.fr>
Cc: caml-list@inria.fr
Subject: Re: How to read floats?
Date: Tue, 8 Aug 2000 04:10:56 +0400 (MSD)	[thread overview]
Message-ID: <Pine.LNX.4.21.0008080408330.923-100000@home.oyster.ru> (raw)
In-Reply-To: <20000807180641.07476@pauillac.inria.fr>

On Mon, 7 Aug 2000, Xavier Leroy wrote:

> > 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.
I overlooked this feature, very stupid, my appology.

> 
> 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 might come in handy too.

> 
> 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,
Sure, my ultra slow Caml function(crafted by a fellow low level coder)
will go away. Thanks!

> - Xavier Leroy
> 

-- 
mailto:malc@pulsesoft.com



      reply	other threads:[~2000-08-08 13:11 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
2000-08-08  0:10   ` malc [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=Pine.LNX.4.21.0008080408330.923-100000@home.oyster.ru \
    --to=malc@pulsesoft.com \
    --cc=Xavier.Leroy@inria.fr \
    --cc=caml-list@inria.fr \
    /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