From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA13391; Fri, 9 Nov 2001 14:53:59 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA13551 for caml-list@pauillac.inria.fr; Fri, 9 Nov 2001 14:53:59 +0100 (MET) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA11409; Fri, 9 Nov 2001 13:18:41 +0100 (MET) Date: Fri, 9 Nov 2001 13:18:41 +0100 From: Xavier Leroy To: Thorsten Ohl Cc: Caml List , "Rafael 'Dido' Sevilla" Subject: Re: [Caml-list] how to split up a Caml float into its component bytes Message-ID: <20011109131841.A5769@pauillac.inria.fr> References: <20011109112909.A9417@team.ph.inter.net> <15339.47220.792529.380015@heplix4.ikp.physik.tu-darmstadt.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <15339.47220.792529.380015@heplix4.ikp.physik.tu-darmstadt.de>; from ohl@hep.tu-darmstadt.de on Fri, Nov 09, 2001 at 12:05:24PM +0100 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > > I'm wondering how I would convert a floating point > > number in OCaml (which I hope I am safe in assuming is IEEE-754) into > > its equivalent bytes. > > AFAIK, you have to write a C function. Starting with OCaml 3.01, the Int64 module provides two functions "bits_of_float" and "float_of_bits" which convert between floats and an int64 matching the underlying 64-bit representation of the float. >>From the int64, extracting individual bytes is a simple matter of shifting and masking. > There is an undocumented primitive > > external float_of_bytes : string -> float = "float_of_bytes" > > defined in byterun/floats.c. Unfortunately: > > a) the inverse is absent > b) on little endian machines one has to swap bytes. a) is correct, b) is not (big endian representation is enforced). Still, the functions from Int64 are less error-prone, e.g. w.r.t. endianness. - Xavier Leroy ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr