Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to print floating point in scientific format? (http://www.velocityreviews.com/forums/t320773-how-to-print-floating-point-in-scientific-format.html)

sdhyok 08-08-2003 07:15 PM

How to print floating point in scientific format?
 
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?

Daehyok Shin

Ben Finney 08-09-2003 12:36 AM

Re: How to print floating point in scientific format?
 
On 8 Aug 2003 12:15:53 -0700, sdhyok wrote:
> I want to change the DEFAULT behavior of python


Then you can modify the Python source to do whatever you want. This is
a bad idea for your current stated purpose, though.

Changing the default behaviour of a native Python type will only be
local to machines that have this altered Python; if you write code that
depends on that, it will break unexpectedly on other machines.

> 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?


Since changing the way native flot object operate is a bad idea, two
ways seem reasonable.

One is to always use output formatting, since it will make your code
clearer.

>>> x = 0.01
>>> print "%E" % x

1.000000E-02

The other way is to subclass the 'float' class to overload its __str__
method. This gives you objects that default to what you want, without
breaking Python.

>>> class SciFloat( float ):

... """ A floating-point number class
... that defaults to scientific representation
... """
... def __init__( self, *args, **kwargs ):
... float.__init__( self, args, kwargs )
... return
... def __str__( self ):
... return "%E" % self
...
>>> if( __name__ == '__main__' ):

... x = SciFloat( 0.01 )
... print x
...
1.000000E-02
>>>


--
\ "I filled my humidifier with wax. Now my room is all shiny." |
`\ -- Steven Wright |
_o__) |
Ben Finney <http://bignose.squidly.org/>

sdhyok 08-09-2003 02:42 PM

Re: How to print floating point in scientific format?
 
I am disappointed that there is no elegant solution for this problem
in python.
As you may notice, my primary usage of python is for scientific
programming.
For the purpose, it is critical for users to flexibly modify the
printing format
of floating points depending on their ranges and precisions.

Think about this case.

>import Numeric as N
>x = N.arange(10.)
>print x

[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
>print "%E"%x

TypeError: float argument required

Even the format string option in print command doesn't work for numpy
array.
This limitation can be a serious handicap in the application of python
for scientific programming.
I like to listen to all of you who are interested in this issue.

Shin, Daehyok

Cousin Stanley 08-09-2003 04:07 PM

Re: How to print floating point in scientific format?
 
The conversion to e format in the print statement
seems to work OK if I iterate over the items in the array ....

>>> import Numeric as N
>>>
>>> x = N.arange( 10. )
>>>
>>> print x

[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
>>>
>>> for this_item in x :

.... print '%e' % this_item
....
0.000000e+000
1.000000e+000
2.000000e+000
3.000000e+000
4.000000e+000
5.000000e+000
6.000000e+000
7.000000e+000
8.000000e+000
9.000000e+000
>>>



--
Cousin Stanley
Human Being
Phoenix, Arizona



Alex Martelli 08-09-2003 04:58 PM

Re: How to print floating point in scientific format?
 
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).


Alex


Andrew Dalke 08-09-2003 06:59 PM

Re: How to print floating point in scientific format?
 
sdhyok:
> As you may notice, my primary usage of python is for scientific
> programming.
> For the purpose, it is critical for users to flexibly modify the
> printing format
> of floating points depending on their ranges and precisions.


Python gives complete control over you can represent numbers.
It just doesn't change the default representation. Users *can*
flexibly modify the printing format.

I can't see why you would want to change it globally. That
just sounds wrong. Eg, I want my app to print the resolution
in Angstroms in the form "%3.2f" while I want the mass values
in "%3.1f". A global setting is a very clumsy way to change that.

> This limitation can be a serious handicap in the application of python
> for scientific programming.
> I like to listen to all of you who are interested in this issue.


I do scientific programming. The lack of specialized formatting
hasn't bothered me at all.

Andrew
dalke@dalkescientific.com



sdhyok 08-10-2003 07:21 PM

Re: How to print floating point in scientific format?
 
Cousin, you are right.
But, do you want to write for loops
whenever you print arrays?
Maybe not if you are a hard-core scientific programmer.

"Cousin Stanley" <CousinStanley@hotmail.com> wrote in message news:<bh361p$u9lvt$1@ID-130333.news.uni-berlin.de>...
> The conversion to e format in the print statement
> seems to work OK if I iterate over the items in the array ....
>
> >>> import Numeric as N
> >>>
> >>> x = N.arange( 10. )
> >>>
> >>> print x

> [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
> >>>
> >>> for this_item in x :

> ... print '%e' % this_item
> ...
> 0.000000e+000
> 1.000000e+000
> 2.000000e+000
> 3.000000e+000
> 4.000000e+000
> 5.000000e+000
> 6.000000e+000
> 7.000000e+000
> 8.000000e+000
> 9.000000e+000
> >>>


sdhyok 08-10-2003 07:27 PM

Re: How to print floating point in scientific format?
 
As you recommend, I won't modify the default behavior of python.
But, still I need a better treatment of python on arrays.

Daehyok Shin

Alex Martelli <aleax@aleax.it> wrote in message news:<aL9Za.34994$an6.1223329@news1.tin.it>...
> 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).
>
>
> Alex


Carl Banks 08-10-2003 08:17 PM

Re: How to print floating point in scientific format?
 
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.


Continually repeating your question isn't going to make a better
treatment magically appear.

Just use the loop method someone suggested.

If you don't like writing a for loop every time, put it in a function.


--
CARL BANKS
"You don't run Microsoft Windows. Microsoft Windows runs you."

Cousin Stanley 08-10-2003 08:32 PM

Re: How to print floating point in scientific format?
 
| Cousin, you are right.
| But, do you want to write for loops
| whenever you print arrays?
|
| Maybe not if you are a hard-core scientific programmer.

Cousin sdhyok ....

I believe that the nature of science and the numerical tools
that it employs for problem solving is highly context dependent
upon the particular problem at hand and that it might be difficult
to find a one-size-fits-all solution for array printing since
the nature of the arrays that are employed and their content
can vary greatly from context to context ....

The dimensonality, homogeniety, and population density
of the arrays employed must be considered as design parameters
in each problem space and treated accordingly ....

However, one can define specialized array functions,
print functions or otherwise, for specific problems
and use those without having to code the loops each time ....

I do have an extensive background in computational chemistry ....

Some would call that discipline hard-core science ....

Others would call it hand-waving in hopeless depths ....

--
Cousin Stanley
Human Being
Phoenix, Arizona




All times are GMT. The time now is 12:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.