[Numpy-discussion] Array printing and another question

Ken Basye kbasye1@jhu....
Tue Apr 8 09:06:15 CDT 2008


Hi Folks,
   I'm pretty new to Numpy, and I hope someone can help me out with a 
couple newbie questions.
   First, I'd like, for diagnostic purposes, to print arrays of floating 
point numbers in a format of my
choosing.  Specifically, I have a formatting function that generates 
strings which represent all the bits
of an FP number exactly, but it formats the exponent, exponent sign, and 
sign separately from the mantissa so I can get a
rough idea of the magnitude at a glance.  We use this to verify that 
certain operations are yielding
exactly the same FP values either across platforms or across algorithmic 
changes.  I'd like to be able
to print numpy arrays in this format.  At this point I've thought of 
three ways to try this.  First, there's
using set_string_function() and then writing my own version of 
array2string() that uses my function
for FP arrays.  OK, but I'd kind of like to avoid the work of 
duplicating the nice way that array2string()
formats arrays of different ranks.  Enticingly, I see code in 
core/arrayprint.py that's looking for an
_format attribute and using it if it exists.  So the second idea is to 
hook into this somehow, but I don't
see how (indeed, I'm not sure when an array ever has an _format 
attribute?).  Finally, I imagine I could
make my own dtype so that array2string will format with %s, then convert 
FP arrays into arrays of
that dtype for printing.  Any ideas which of these would be better, or 
other ideas, would be most
appreciated.
  Second, I wanted to find out how numpy developers and experts feel 
about cross-platform differences
in FP behavior.  Is this considered unavoidable and expected, a bug, or 
somewhere in between? 
Until recently I was about to get exact matches everywhere, but I now 
have at least one example of
a difference.  I should clarify that I'm not talking about 
processor-family dependency; all my machines
are x86 at this point.  Indeed, the differences appear across virtual 
machines running different OSs on
the same physical machine. 
   Thanks in advance,
       Ken Basye
       kbasye1@jhu.edu


More information about the Numpy-discussion mailing list