From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA12334 for caml-red; Tue, 8 Aug 2000 15:11:03 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA06671 for ; Tue, 8 Aug 2000 02:12:27 +0200 (MET DST) Received: from pinochet.cityline.ru (pinochet.cityline.ru [195.46.160.34]) by concorde.inria.fr (8.10.0/8.10.0) with ESMTP id e780CPL27590; Tue, 8 Aug 2000 02:12:26 +0200 (MET DST) Received: from home.oyster.ru (27.172.10.dn.dialup.cityline.ru [195.46.172.27]) by pinochet.cityline.ru (8.10.2/t/08-Oct-1998) with ESMTP id e780ARg09962; Tue, 8 Aug 2000 04:10:30 +0400 (MSD) Date: Tue, 8 Aug 2000 04:10:56 +0400 (MSD) From: malc X-Sender: malc@home.oyster.ru To: Xavier Leroy cc: caml-list@inria.fr Subject: Re: How to read floats? In-Reply-To: <20000807180641.07476@pauillac.inria.fr> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: weis@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 > #include > > 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