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?

 
 
sdhyok
Guest
Posts: n/a
 
      08-08-2003
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
 
Reply With Quote
 
 
 
 
Ben Finney
Guest
Posts: n/a
 
      08-09-2003
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/>
 
Reply With Quote
 
 
 
 
sdhyok
Guest
Posts: n/a
 
      08-09-2003
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
 
Reply With Quote
 
Cousin Stanley
Guest
Posts: n/a
 
      08-09-2003
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


 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      08-09-2003
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

 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      08-09-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
sdhyok
Guest
Posts: n/a
 
      08-10-2003
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" <(E-Mail Removed)> wrote in message news:<bh361p$u9lvt$(E-Mail Removed)-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
> >>>

 
Reply With Quote
 
sdhyok
Guest
Posts: n/a
 
      08-10-2003
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 <(E-Mail Removed)> wrote in message news:<aL9Za.34994$(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?

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

 
Reply With Quote
 
Carl Banks
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.


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."
 
Reply With Quote
 
Cousin Stanley
Guest
Posts: n/a
 
      08-10-2003
| 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


 
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