Re: [Tutor] python precision output?

2007-12-06 Thread Kent Johnson
Andre Walker-Loud wrote:
> To reiterate, I need str(value) to be written to my file with 16  
> digits of precision...???

You can use string formatting to specify exactly how many decimal places 
to include:

In [1]: v=1.0/7
In [2]: v
Out[2]: 0.14285714285714285
In [3]: str(v)
Out[3]: '0.142857142857'
In [4]: '%.16f' % v
Out[4]: '0.1428571428571428'

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python precision output?

2007-12-06 Thread Andre Walker-Loud
> if you want accuracy and are willing to sacrifice the total range of
> numbers that Python's IEEE754 double-precision floats give you, then
> use the decimal.Decimal class instead -- better precision, smaller
> range.
>
> however, if you wish to stick with floats, use the string format
> operator and tell it you want 17 places after the decimal point:
>
 x=7./13
 x
> 0.53846153846153844
 str(x)
> '0.538461538462'
 '%.17f' % x
> '0.53846153846153844'
>
> hope this helps!

This did the trick!  thanks,

Andre




> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> http://corepython.com
>
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python precision output?

2007-12-06 Thread wesley chun
> The problem I am having is getting python to write number into this
> input file, keeping 16 digits of precision.  I have played around
> interactively, and see that python default prints 17 digits of
> precision to the screen, but when I use a replace command to write
> into the input file, it only prints 12 digits of precision.  The
> relevant snipit of my script is
>:
> To reiterate, I need str(value) to be written to my file with 16
> digits of precision...???

if you want accuracy and are willing to sacrifice the total range of
numbers that Python's IEEE754 double-precision floats give you, then
use the decimal.Decimal class instead -- better precision, smaller
range.

however, if you wish to stick with floats, use the string format
operator and tell it you want 17 places after the decimal point:

>>> x=7./13
>>> x
0.53846153846153844
>>> str(x)
'0.538461538462'
>>> '%.17f' % x
'0.53846153846153844'

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python precision output?

2007-12-06 Thread Eric Brunson
Andre Walker-Loud wrote:
> Hi there,
>   

Hi Andre,

First of all, please don't start a new thread by replying to an existing 
thread, RFC compliant email readers will thread your post along with the 
original posting based on headers other than the Subject.  :-)

I don't think you'll ever get satisfactory precision using binary 
floating point (the default for python's float type).  Take a look at 
the decimal module, I believe it will give you much better results for 
your requirements.

Here is the module documentation:  
http://docs.python.org/lib/module-decimal.html

And an example of it's usage:

>>> getcontext().prec = 6
>>> Decimal(1) / Decimal(7)
Decimal("0.142857")
>>> getcontext().prec = 28
>>> Decimal(1) / Decimal(7)
Decimal("0.1428571428571428571428571429")


Please post back if that doesn't help you out.

Sincerely,
e.

> I am using python to do some scripting.  In particular, I am using it  
> to run some jobs which require precision inputs.  I do this by having  
> python write an input file, which I then feed to some other program.
>
> The problem I am having is getting python to write number into this  
> input file, keeping 16 digits of precision.  I have played around  
> interactively, and see that python default prints 17 digits of  
> precision to the screen, but when I use a replace command to write  
> into the input file, it only prints 12 digits of precision.  The  
> relevant snipit of my script is
>
> value = float( int(ai) * 6 * math.pi / (int(L)*int(T))
> replace = {'VALUE':str(value)}
> ini_file = open('generic_ini').read()
> f=open('my_input.xml','w')
> f.write(ini_file % replace)
> f.close()
>
> where, "ai", "L" and "T" are process dependent numbers defined in my  
> script, and the output "my_input.xml", is just an xml file I later  
> feed to another program, and this is why I replace 'VALUE' with a  
> string.
>
> To reiterate, I need str(value) to be written to my file with 16  
> digits of precision...???
>
>
> Thanks,
> Andre
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python precision output?

2007-12-06 Thread Andre Walker-Loud
Hi there,

I am using python to do some scripting.  In particular, I am using it  
to run some jobs which require precision inputs.  I do this by having  
python write an input file, which I then feed to some other program.

The problem I am having is getting python to write number into this  
input file, keeping 16 digits of precision.  I have played around  
interactively, and see that python default prints 17 digits of  
precision to the screen, but when I use a replace command to write  
into the input file, it only prints 12 digits of precision.  The  
relevant snipit of my script is

value = float( int(ai) * 6 * math.pi / (int(L)*int(T))
replace = {'VALUE':str(value)}
ini_file = open('generic_ini').read()
f=open('my_input.xml','w')
f.write(ini_file % replace)
f.close()

where, "ai", "L" and "T" are process dependent numbers defined in my  
script, and the output "my_input.xml", is just an xml file I later  
feed to another program, and this is why I replace 'VALUE' with a  
string.

To reiterate, I need str(value) to be written to my file with 16  
digits of precision...???


Thanks,
Andre
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor