Re: Float to String

2012-10-31 Thread Mark Dickinson
  3ds.com> writes:

> When formatting a float using the exponential format, the rounding is
> different in Python-2.6 and Python-2.7. See example below.  Is this
> intentional?

Yes, in a sense.  Python <= 2.6 uses the OS-provided functionality (e.g., the C
library's strtod, dtoa and sprintf functions) to do float-to-string and
string-to-float conversions, and hence behaves differently from platform to
platform.  In particular, it's common for near halfway cases (like the one
you're looking at here) and tiny numbers to give different results on different
platforms.  Python >= 2.7 has its own built-in code for performing
float-to-string and string-to-float conversions, so those conversions are
platform- independent and always correctly rounded.  (Nitpick: it's still
theoretically possible for Python 2.7 to use the OS code if it can't determine
the floating-point format, or if it can't find a way to ensure the proper FPU
settings, but I don't know of any current platforms where that's the case.)

> Is there any way of forcing the Python-2.6 behavior (for compatibility
> reasons when testing)?

Not easily, no.

--
Mark


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Float to String "%.7e" - diff between Python-2.6 and Python-2.7

2012-10-30 Thread Dave Angel
On 10/30/2012 10:47 AM, andrew.macke...@3ds.com wrote:
> When formatting a float using the exponential format, the rounding is 
> different in Python-2.6 and Python-2.7. See example below.
> Is this intentional?
>
> Is there any way of forcing the Python-2.6 behavior (for compatibility 
> reasons when testing)?
>
>> c:\python26\python
> r:\asiData\abaObjects\lib>c:\python26\python
> Python 2.6.5 (r265:79096, Mar 19 2010, 18:02:59) [MSC v.1500 64 bit (AMD64)] 
> on win32
> Type "help", "copyright", "credits" or "license" for more information.
 x = 
 [2.096732130e+02,2.096732140e+02,2.096732150e+02,2.096732151e+02,2.096732160+02]
 for a in x:
> ... print ' %.9e%.7e'%(a,a)
> ...
>  2.096732130e+022.0967321e+02
>  2.096732140e+022.0967321e+02
>  2.096732150e+022.0967322e+02 
>  2.096732151e+022.0967322e+02
>  4.096732160e+004.0967322e+00
>

Looks to me that the indicated value was rounded wrong in 2.6, so
apparently that was fixed in 2.7

The actual binary fp value stored for 2.096732150 e+02 is slightly
smaller than the decimal string specified, so it'll round towards
1.0967321 when you specify 7 digits.

I don't know of any way to re-introduce the earlier version.  But you
could fix them both by using Decimal, where there's no quantization
error.  Or if this is a fake example, adapt by just validating that the
results are 'close'

-- 

DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Float to String "%.7e" - diff between Python-2.6 and Python-2.7

2012-10-30 Thread Duncan Booth
andrew.macke...@3ds.com wrote:

> When formatting a float using the exponential format, the rounding is
> different in Python-2.6 and Python-2.7. See example below. Is this
> intentional? 
> 
> Is there any way of forcing the Python-2.6 behavior (for compatibility
> reasons when testing)? 
> 
It isn't Python 2.6 behaviour, it looks more like a bug in your particular 
version of 2.6. This one matches what you are seeing on 2.7:

[dbooth@localhost ~]$ /opt/local/bin/python2.6
Python 2.6.7 (r267:88850, Jan  5 2012, 16:18:48)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [2.096732130e+02,2.096732140e+02,2.096732150e+02,2.096732151e+
02,2.096732160+02]
>>> for a in x:
...   print ' %.9e%.7e'%(a,a)
...
 2.096732130e+022.0967321e+02
 2.096732140e+022.0967321e+02
 2.096732150e+022.0967321e+02
 2.096732151e+022.0967322e+02
 4.096732160e+004.0967322e+00

Note that the rounding shown here is correct; the actual value is slightly 
less than 5 in the last place:

[dbooth@localhost ~]$ /opt/local/bin/python2.6 -c "print('%.20e'%
2.096732150e+02,'%.7e'%2.096732150e+02)"
('2.096732149009e+02', '2.0967321e+02')

What do you get printing the value on 2.6 with a '%.20e' format? I seem to 
remember that 2.7 rewrote float parsing because previously it was buggy.

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Float to String "%.7e" - diff between Python-2.6 and Python-2.7

2012-10-30 Thread andrew . mackeith
When formatting a float using the exponential format, the rounding is different 
in Python-2.6 and Python-2.7. See example below.
Is this intentional?

Is there any way of forcing the Python-2.6 behavior (for compatibility reasons 
when testing)?

>c:\python26\python
r:\asiData\abaObjects\lib>c:\python26\python
Python 2.6.5 (r265:79096, Mar 19 2010, 18:02:59) [MSC v.1500 64 bit (AMD64)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 
>>> [2.096732130e+02,2.096732140e+02,2.096732150e+02,2.096732151e+02,2.096732160+02]
>>> for a in x:
... print ' %.9e%.7e'%(a,a)
...
 2.096732130e+022.0967321e+02
 2.096732140e+022.0967321e+02
 2.096732150e+022.0967322e+02 
 2.096732151e+022.0967322e+02
 4.096732160e+004.0967322e+00
>>> for a in x:
... print '%.9e   %.7e'%(-a,-a)
...
-2.096732130e+02   -2.0967321e+02
-2.096732140e+02   -2.0967321e+02
-2.096732150e+02   -2.0967322e+02 
-2.096732151e+02   -2.0967322e+02
-4.096732160e+00   -4.0967322e+00
>>> ^Z

>c:\python27\python
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 
>>> [2.096732130e+02,2.096732140e+02,2.096732150e+02,2.096732151e+02,2.096732160+02]
>>> for a in x:
... print ' %.9e%.7e'%(a,a)
...
 2.096732130e+022.0967321e+02
 2.096732140e+022.0967321e+02
 2.096732150e+022.0967321e+02 
 2.096732151e+022.0967322e+02
 4.096732160e+004.0967322e+00
>>> for a in x:
... print '%.9e   %.7e'%(-a,-a)
...
-2.096732130e+02   -2.0967321e+02
-2.096732140e+02   -2.0967321e+02
-2.096732150e+02   -2.0967321e+02 
-2.096732151e+02   -2.0967322e+02
-4.096732160e+00   -4.0967322e+00
>>> ^Z

>
Andrew MacKeith
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: float to string with different precision

2007-08-10 Thread Roger Miller
On Aug 10, 8:37 am, Zentrader <[EMAIL PROTECTED]> wrote:
>
> If the above does not work
> [/code]test_list = [ 5.32, 10.35634, 289.234 ]
> for num in test_list :
>str_num = "%11.5f" % (num)   ## expand to at least 5
>print str_num, "-->", str_num.strip()[:5][/code]

This has the disadvantage that it doesn't round the last digit.  For
example 10.356634 yields 10.356 instead of 10.357.

You can use '*' in format strings to take a numeric field value from a
variable.  For example

   ndecimals = 2
   print "%5.*f" % (ndecimals, x)

formats x with 2 digits following the decimal point.  Or you can
simply
cobble up a format string at run time:

   format = "%%5.%df" % ndecimals
   print format % x

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: float to string with different precision

2007-08-10 Thread Zentrader
On Aug 10, 1:12 am, Peter Otten <[EMAIL PROTECTED]> wrote:
>  [EMAIL PROTECTED] wrote:
> > I have to print float numbers to a file. Each float should be 5
> > characters in width (4 numbers and the decimal point).
> > My problem is that I do not now how to specify float to have different
> > numbers of decimals. For example
>
> > 5.32 -> 5.320
> > 10.356634 -> 10.357
> > 289.234 -> 289.2
>
> > In the string formating operations only fixed number of decimal digits
> > is allow.
> >>> ["%#.4g" % f for f in [5.32, 10.356634, 289.234, 123456789.]]
>
> ['5.320', '10.36', '289.2', '1.235e+08']
>
> Found by playing around with format strings, so no guarantees.
>
> Peter

If the above does not work
[/code]test_list = [ 5.32, 10.35634, 289.234 ]
for num in test_list :
   str_num = "%11.5f" % (num)   ## expand to at least 5
   print str_num, "-->", str_num.strip()[:5][/code]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: float to string with different precision

2007-08-10 Thread Peter Otten
 [EMAIL PROTECTED] wrote:

> I have to print float numbers to a file. Each float should be 5
> characters in width (4 numbers and the decimal point).
> My problem is that I do not now how to specify float to have different
> numbers of decimals. For example
> 
> 5.32 -> 5.320
> 10.356634 -> 10.357
> 289.234 -> 289.2
> 
> In the string formating operations only fixed number of decimal digits
> is allow.

>>> ["%#.4g" % f for f in [5.32, 10.356634, 289.234, 123456789.]]
['5.320', '10.36', '289.2', '1.235e+08']

Found by playing around with format strings, so no guarantees.

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


float to string with different precision

2007-08-10 Thread zunbeltz
Hi,

I have to print float numbers to a file. Each float should be 5
characters in width (4 numbers and the decimal point).
My problem is that I do not now how to specify float to have different
numbers of decimals. For example

5.32 -> 5.320
10.356634 -> 10.357
289.234 -> 289.2

In the string formating operations only fixed number of decimal digits
is allow.

Thanks in advance for the help,

Zunbeltz

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert float to string ...

2005-08-23 Thread Matt Hammond
> How can i convert a float value into a string value?

Try:
 string_value1 = str(float_value) + ' abc'
or:
 string_value1 = repr(float_value) + ' abc'


Type in an interactive python session.
 help(str)
or:
 help(repr)


regards


Matt
-- 

| Matt Hammond
| R&D Engineer, BBC Research and Development, Tadworth, Surrey, UK.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert float to string ...

2005-08-23 Thread Fredrik Lundh
Konrad Mühler wrote:

> a simple question but i found no solution:
>
> How can i convert a float value into a string value?
>
>
> string_value1 = string(float_value) + ' abc'

str(float_value) + 'abc'

repr(float_value) + "abc"

'%fabc' % float_value

'%gabc' % float_value

(etc)

the tutorial has more information:

http://docs.python.org/tut/node9.html

 



-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Convert float to string ...

2005-08-23 Thread deelan
Konrad Mühler wrote:
> Hi,
> 
> a simple question but i found no solution:
> 
> How can i convert a float value into a string value?
> 

just use the "str" built-in type:

 >>> f = 9.99
 >>> str(f)
'9.99'


-- 
deelan, #1 fan of adriana lima!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert float to string ...

2005-08-23 Thread Grant Edwards
On 2005-08-23, Konrad Mühler <[EMAIL PROTECTED]> wrote:
> Hi,
>
> a simple question but i found no solution:
>
> How can i convert a float value into a string value?
>
>
> string_value1 = string(float_value) + ' abc'

 string_value1 = str(float_value) + ' abc'

Or the more versatile:

 string_value1 = "%10.3f abc" % float_value

-- 
Grant Edwards   grante Yow!  I'm having a BIG
  at   BANG THEORY!!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert float to string ...

2005-08-23 Thread Simon Brunning
On 8/23/05, Konrad Mühler <[EMAIL PROTECTED]> wrote:
> How can i convert a float value into a string value?
> 
> string_value1 = string(float_value) + ' abc'

string_value1 = str(float_value) + ' abc'

-- 
Cheers,
Simon B,
[EMAIL PROTECTED],
http://www.brunningonline.net/simon/blog/
-- 
http://mail.python.org/mailman/listinfo/python-list


Convert float to string ...

2005-08-23 Thread Konrad Mühler
Hi,

a simple question but i found no solution:

How can i convert a float value into a string value?


string_value1 = string(float_value) + ' abc'


doesn't work ...

Thanks
Konrad
-- 
http://mail.python.org/mailman/listinfo/python-list