Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to print floating point in scientific format?

Reply
Thread Tools

How to print floating point in scientific format?

 
 
Bengt Richter
Guest
Posts: n/a
 
      08-10-2003
On Sat, 09 Aug 2003 16:58:46 GMT, Alex Martelli <(E-Mail Removed)> wrote:

>sdhyok wrote:
>
>> I want to change the DEFAULT behavior of python
>> to print out all floating points in scientific format?
>> For instance,
>>
>>>x=0.01
>>>print x

>> 1.000000E-2 #Like print "%E"%x
>>
>> How can I do it?

>
>You need to download Python's sources, modify them, and build a modified
>Python interpreter and libraries that impose the behavior you want rather
>than what the normal Python interpreter and libraries do.
>
>Specifically, look at function format_float, line 233 of file
>Objects/floatobject.c in the current 2.3 maintenance branch for example.
>Currently it formats the float with "%.*g" [variable precision passed
>in as an argument to format_float]; if you want to use a different C level
>format string, that string is the one you need to change.
>
>Of course, that's likely to break some of the tests in Python's unit-tests
>suite, so you'll probably want to modify those, too. And then, you get to
>maintain your "slightly divergent" Python yourself forevermore. I do not
>think there is much likelihood that a patch in this regard would be
>accepted in the Python core, even if you made it flexible enough to keep
>the current behavior by default and change it only upon specific request
>(e.g., use a variable string for the format, and let the Python coder
>modify the letter in it, only, keeping "%.*g" as the default but letting
>the 'g' be changed) -- such "big global" settings, which would let one
>idiosyncratic library module modify Python behavior enough to break other
>innocent modules, are looked at with disfavour, for reasons that should
>be obvious (each and every such 'big global' _damages_ Python's suitability
>for writing very large, multi-authors applications -- that suitability is
>currently very high, and _extremely_ convincing arguments would need to
>be brought to bear in order to convince Guido to deliberately lower it).
>
>

OTOH, why not a sys.printhook ? There is a kind of precedent in

>>> import sys
>>> def dh(obj):

... if isinstance(obj, (int, long, float)): return sys.__displayhook__('%e'%float(obj))
... else: return sys.__displayhook__(obj)
...
>>> sys.displayhook = dh
>>> 1

'1.000000e+000'
>>> _

'1.000000e+000'
>>> 2L

'2.000000e+000'
>>> 3.

'3.000000e+000'
>>> 1, 2L, 3.

(1, 2L, 3.0)

I didn't bother with recursive list and tuple mods. That would be up to the printhook user.
ISTM a printhook would be easy to implement and not disruptive overall, whatever silliness
it might inspire in particular individuals

Actually, I think there could even be some good use for it, since you could do things with
the object stream that you couldn't do by intercepting strings in sys.stdout.write.

Regards,
Bengt Richter
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      08-10-2003
sdhyok wrote:

> As you recommend, I won't modify the default behavior of python.
> But, still I need a better treatment of python on arrays.


Arrays are supplied by extension modules, most popularly Numeric.

If you're doing scientific computations in Python and use arrays but
not Numeric (or its slated-to-be-replacement numarray, of which I
don't know much yet; or the many things layered atop of and onside
of Numeric, such as scipy), switch. You'll be happy you did.

I've never heard a scientific programmer complain too badly about
how Numeric treats arrays, whether they were coming from Fortran
(the typical case), other higher-level languages, or general purpose
languages not really suited for scientific computation (such as C).

You seem to be focused on readable display of arrays (quite an
unusual focus for scientific programming). So, worst case, it's trivial
to write a general purpose function that takes an array of ANY size
and rank and emits it in the way you prefer -- easier than in any
other language commonly used for scientific programming.

So, you don't need anything from Python -- just write that blessed
function and scientifically program to your heart's content.


Alex

 
Reply With Quote
 
 
 
 
David M. Cooke
Guest
Posts: n/a
 
      08-11-2003
At some point, Alex Martelli <(E-Mail Removed)> wrote:
> sdhyok wrote:
>
>> As you recommend, I won't modify the default behavior of python.
>> But, still I need a better treatment of python on arrays.

>
> You seem to be focused on readable display of arrays (quite an
> unusual focus for scientific programming). So, worst case, it's trivial
> to write a general purpose function that takes an array of ANY size
> and rank and emits it in the way you prefer -- easier than in any
> other language commonly used for scientific programming.


With Numeric at least, you can set globally the functions used for
str() and repr() on arrays, giving exactly this, without requiring a
separate function for representations. Have a look at Numeric.py,
multiarray.set_string_function(), and ArrayPrinter.py from the Numeric
distribution.

The simple way of controlling output with Numeric is to use the
variables sys.output_line_width, sys.float_output_precision and
sys.float_output_supress_small (if true, replace in the output numbers that
are much smaller than the rest with zero, which is very nice when most of
your numbers are 1.0, and you've got some annoying 1.8342e-17 that you want
to ignore). These are added by Numeric to the sys module.

With numarray, you could subclass the array object, write your own
__str__ and __repr__ methods, and use that instead.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      08-11-2003
On 10 Aug 2003 20:41:35 -0700, sdhyok wrote:
> Writing my own function is definitely one choice.
> But, my point is that if some of us are really serious about
> scientific/engineering programming, we must have a common function or
> command to print out whole array elements easily with any format we
> want.


We eagerly await your patches for improving the Numeric code in whatever
way you want.

--
\ "If I melt dry ice, can I swim without getting wet?" -- Steven |
`\ Wright |
_o__) |
Ben Finney <http://bignose.squidly.org/>
 
Reply With Quote
 
sdhyok
Guest
Posts: n/a
 
      08-11-2003
Writing my own function is definitely one choice.
But, my point is that if some of us are really serious
about scientific/engineering programming,
we must have a common function or command
to print out whole array elements easily with any format we want.

It can be in Numeric (or numarray) package like,

import Numeric as N
N.print(array, format="%.2E")


Daehyok Shin

http://www.velocityreviews.com/forums/(E-Mail Removed) (Bengt Richter) wrote in message news:<bh6bq6$p1u$0@216.39.172.122>...
> On 8 Aug 2003 12:15:53 -0700, (E-Mail Removed) (sdhyok) wrote:
>
> >I want to change the DEFAULT behavior of python
> >to print out all floating points in scientific format?
> >For instance,
> >
> >>x=0.01
> >>print x

> >1.000000E-2 #Like print "%E"%x
> >
> >How can I do it?
> >

> Do you have to use the print statement per se for output?
> I.e., why couldn't you write
>
> sciPrint( x, whatever, etc )
>
> instead of
>
> print x, whatever, etc
>
> Then you could customize sciPrint as you please.
> Or do you have to change the behaviour of existing modules without
> changing their source?
>
> Regards,
> Bengt Richter

 
Reply With Quote
 
sdhyok
Guest
Posts: n/a
 
      08-11-2003
Thanks, Alex.

snip

> If you're doing scientific computations in Python and use arrays but
> not Numeric (or its slated-to-be-replacement numarray, of which I
> don't know much yet; or the many things layered atop of and onside
> of Numeric, such as scipy), switch. You'll be happy you did.

I refer all sequences, but, as you say, particularly numpy arrays.

> I've never heard a scientific programmer complain too badly about
> how Numeric treats arrays, whether they were coming from Fortran
> (the typical case), other higher-level languages, or general purpose
> languages not really suited for scientific computation (such as C).


Right, we can use scientific binary formats, like NetCDF, HDF.
But, we often want to read the data in our naked eyes
and exchange them with spreadsheet.

>
> You seem to be focused on readable display of arrays (quite an
> unusual focus for scientific programming). So, worst case, it's trivial
> to write a general purpose function that takes an array of ANY size
> and rank and emits it in the way you prefer -- easier than in any
> other language commonly used for scientific programming.
>
> So, you don't need anything from Python -- just write that blessed
> function and scientifically program to your heart's content.


Trivial to create my own function.
But, surprisingly no common function or command for the work.
Maybe, one solution is

import Numeric as N
N.print(array, format="%.2E")


Daehyok Shin
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      08-11-2003
sdhyok wrote:
...
>> I've never heard a scientific programmer complain too badly about
>> how Numeric treats arrays, whether they were coming from Fortran
>> (the typical case), other higher-level languages, or general purpose
>> languages not really suited for scientific computation (such as C).

>
> Right, we can use scientific binary formats, like NetCDF, HDF.
> But, we often want to read the data in our naked eyes
> and exchange them with spreadsheet.


For "exchange with spreadsheets" see the csv module (new in 2.3), e.g.:

>>> import csv
>>> import sys
>>> csv.writer(sys.stdout).writerow([ 100.+i for i in range(3, ])

103.0,104.0,105.0,106.0,107.0
>>>


It doesn't, of course, force scientific format (it uses the general
format) -- spreadsheets don't need that. However, it does have the
concept of a "Dialect" (here, I've let the dialect default to 'excel'),
a bundle of formatting parameters that tweak the output into the best
form for a given spreadsheet; that doesn't provide the specific bit
of formatting control you want, but it gives what's clearly the right
"hook" on which to hang any kind of formatting control. If you need
to pursue "exchange with spreadsheet" and you have a use-case for
needing scientific format in such a setting, then proposing a patch
to module csv [having the dialect optionally record the preferred
way to format floating point numbers] might stand a good chance.

Right now, _csv.c [line 1126 in the sources) just transforms the
fields it's emitting with PyObject_Str(field) -- equivalent to the
Python call str(field), just as the print statement does. But just
as it nows checks dialect->quoting before coming to that, it might
well check some other field of the '*dialect* structure in order
to influence output formatting selectively -- *IF* good use cases
could be found and verified where a spreadsheet's behavior can be
made better by special formatting of numbers fed to it.


>> You seem to be focused on readable display of arrays (quite an
>> unusual focus for scientific programming). So, worst case, it's trivial
>> to write a general purpose function that takes an array of ANY size
>> and rank and emits it in the way you prefer -- easier than in any
>> other language commonly used for scientific programming.
>>
>> So, you don't need anything from Python -- just write that blessed
>> function and scientifically program to your heart's content.

>
> Trivial to create my own function.


We agree.

> But, surprisingly no common function or command for the work.


Actually, Numeric.array2string IS exactly such a "common function
or command". It's been written 7 years ago by Konrad Hinsen and
happily used ever since by thousands of happy users of Numeric --
how much more common than this can you get?

However, among its very numerous formatting parameters, it does
NOT have one to let you force exponential format as opposed to
fixedpoint format -- it always decides that tidbit (i.e. whether
an 'e' or 'f' is ultimately used in the format string) by itself.

Clearly, no Numeric user has had substantial need for this kind
of forced output formatting over these many years -- which I do
not find at all surprising. Still, array2string is a Python
coded function -- see its sources in Numeric/ArrayPrinter.py --
so that experimenting with changes to it is trivial, and maybe
the Numeric maintainers will look with favour upon a patch,
should you decide to offer one. It might be as simple as adding
yet one more optional parameter to the array2string function,
say "forcedformat" defaulting to None, passing it on to the
underlying internal _floatFormat function it calls to determine
the format string, and having _floatFormat just use it, if not
None, instead of determining the format itself. I would
suggest you make a copy of ArrayPrinter.py, tweak it to your
own satisfaction, then send the resulting patch to the Numeric
maintainers proposing inclusion in Numeric's next release.

> Maybe, one solution is
>
> import Numeric as N
> N.print(array, format="%.2E")


In my opinion, the solution I just sketched for you, i.e.:

print N.array2string(array, precision=2, forcedformat="E")

stands a much better chance than the addition of a new function
that does direct printing (rather than the more useful and
flexibile formatting-to-string) and merges precision and
formatcharacter into one argument (note that array2string
does already accept a precision argument) -- even without
considering the fact that 'print' is a keyword and thus not
usable as a function-name.


Alex

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
Converting floating point to string in non-scientific format Madhusudhanan Chandrasekaran Python 2 05-01-2006 03:49 PM
converting floating point to fixed point H aka N VHDL 15 03-02-2006 02:26 PM



Advertisments