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 PAA12327 for caml-red; Tue, 8 Aug 2000 15:10:24 +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 SAA02762 for ; Mon, 7 Aug 2000 18:06:44 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.10.0/8.10.0) with ESMTP id e77G6fL23148; Mon, 7 Aug 2000 18:06:41 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id SAA02758; Mon, 7 Aug 2000 18:06:41 +0200 (MET DST) Message-ID: <20000807180641.07476@pauillac.inria.fr> Date: Mon, 7 Aug 2000 18:06:41 +0200 From: Xavier Leroy To: malc , caml-list@inria.fr Subject: Re: How to read floats? References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.89.1 In-Reply-To: ; from malc on Wed, Aug 02, 2000 at 03:15:27AM +0400 Sender: weis@pauillac.inria.fr > 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 #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 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