MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

IEEE single precision number.

  • To: mathgroup at
  • Subject: [mg51782] IEEE single precision number.
  • From: Vangelis Marinakis <V.Marinakis at>
  • Date: Tue, 2 Nov 2004 02:05:02 -0500 (EST)
  • Sender: owner-wri-mathgroup at

Dear everyone,

Looking at the internet I found this email:



I want to convert a group/stream of Bytes to various internal
Mathematica numerical types, particularly into Real (machine precision
number).  The motivation is the fast reading of binary files.

I don't want to deal with the Std Package
Utilities`BinaryFiles`ReadBinary or Developer'BinaryImport, which do
have the conversion utilities (e.g. into Real32, etc), since they are
SLOW or otherwise clunky.

When employing the suggested fast reading of binary files: 

There are numerous means of reading data into Mathematica from binary
files.  However, most of them are too slow.  Through experience and
some newsgroup research, I've found the fast easy way:

stream = OpenRead["Cals9", DOSTextFormat -> False];
   (* Note:  this DOSTextFormat->False option is key to using
ReadList[] in binary mode; otherwise, it wants to digest text.  Readin
Byte chunks, then post-format.  Remarkably, the DosTextFormat option
is not mentioned/discussed anywhere in the Mathematica book or Help
Browser *)

SetStreamPosition[stream, 78*131072];
     (* Position to desired read starting location within file...count
is in Bytes, and is index base = 0  *).

data = ReadList[stream, Byte, 131072];
      (* Reads 131072 Bytes into linear List "data" *)


you end up with a list of Bytes.  Converting it to 16-bit integers is

data = Map[(#1[[1]] + 256*#1[[2]] & ), Partition[data, 2]];
  (* takes original Byte List "data" and makes 16-bit "words", writing
over List "data".  Result is half-as-long List of words.  *)

...but how would one convert it to real numbers (given that every 4
bytes comprises an IEEE single-precision number)?


franki at


Does anyone no the answer, because I have exactly the same problem!


Vangelis Marinakis.

  • Prev by Date: NMaximize woes
  • Next by Date: Garbage collection problem
  • Previous by thread: Re: NMaximize woes
  • Next by thread: Re: IEEE single precision number.