* How to read floats?
@ 2000-08-01 23:15 malc
  2000-08-07 16:06 ` Xavier Leroy
  0 siblings, 1 reply; 3+ messages in thread
From: malc @ 2000-08-01 23:15 UTC (permalink / raw)
  To: caml-list
I need to read binary data from files, C floats (32bit) in particular,
maybe someone here already got code to do that? 
-- 
mailto:malc@pulsesoft.com
^ permalink raw reply	[flat|nested] 3+ messages in thread
* Re: How to read floats?
  2000-08-01 23:15 How to read floats? malc
@ 2000-08-07 16:06 ` Xavier Leroy
  2000-08-08  0:10   ` malc
  0 siblings, 1 reply; 3+ messages in thread
From: Xavier Leroy @ 2000-08-07 16:06 UTC (permalink / raw)
  To: malc, caml-list
> 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
^ permalink raw reply	[flat|nested] 3+ messages in thread
* Re: How to read floats?
  2000-08-07 16:06 ` Xavier Leroy
@ 2000-08-08  0:10   ` malc
  0 siblings, 0 replies; 3+ messages in thread
From: malc @ 2000-08-08  0:10 UTC (permalink / raw)
  To: Xavier Leroy; +Cc: caml-list
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
^ permalink raw reply	[flat|nested] 3+ messages in thread
end of thread, other threads:[~2000-08-08 13:11 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-08-01 23:15 How to read floats? malc
2000-08-07 16:06 ` Xavier Leroy
2000-08-08  0:10   ` malc
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox