RE: Undocumented alternate form for %#f ?



Ahh, cool... Thanks for the explanation!

Do you want to help develop Dynamic languages on CLR? (http://members.microsoft.com/careers/search/details.aspx?JobID=6D4754DE-11F0-45DF-8B78-DC1B43134038)
-----Original Message-----
From: python-list-bounces+dinov=microsoft.com@xxxxxxxxxx [mailto:python-list-bounces+dinov=microsoft.com@xxxxxxxxxx] On Behalf Of Dave Hughes
Sent: Friday, April 28, 2006 1:00 PM
To: python-list@xxxxxxxxxx
Subject: Re: Undocumented alternate form for %#f ?

Dino Viehland wrote:

I'm assuming this is by-design, but it doesn't appear to be
documented:

'%8.f' % (-1)
' -1'
'%#8.f' % (-1)
' -1.'


The docs list the alternate forms, but there isn't one listed for
f/F. It would seem the alternate form for floating points is
truncate & round the floating point value, but always display the .
at the end. Is that correct?

The Python % operator follows the C sprintf function pretty darn
closely in behaviour (hardly surprising really, though I've never
peeked at the implementation). Hence "man sprintf" can provide some
clues here. From man sprintf on my Linux box:

#
The value should be converted to an ``alternate form''. For o
conversions, the first character of the output string is made
zero (by prefixing a 0 if it was not zero already). For x and X
conversions, a non-zero result has the string `0x' (or `0X' for
X conversions) prepended to it. For a, A, e, E, f, F, g, and G
conversions, the result will always contain a decimal point,
even if no digits follow it (normally, a decimal point appears
in the results of those conversions only if a digit follows).
For g and G conversions, trailing zeros are not removed from the
result as they would otherwise be. For other conversions, the
result is undefined.

Hence, I don't think it's the # doing the truncating here, but it
certainly is producing the mandatory decimal point. If you get rid of
the "." in the specification, it uses the default decimal precision (6):

"%8f" % (-1)
'-1.000000'
"%#8f" % (-1)
'-1.000000'

No difference with the alternate specification here as the precision is
non-zero. Again, from man sprintf:

The precision
[snip]
If the precision is given as
just `.', or the precision is negative, the precision is taken to be
zero. This gives the minimum number of digits to appear for d, i, o,
u, x, and X conversions, the number of digits to appear after the radix
character for a, A, e, E, f, and F conversions, the maximum number of
significant digits for g and G conversions, or the maximum number of
characters to be printed from a string for s and S conversions.


HTH,

Dave.
--

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



Relevant Pages

  • Re: (decimal) 1.1 versus 1.1M
    ... so conversions between the two may lose information. ... Double, however, has less precision overall than decimal. ... so it runs out of digits of precision much more ... double value to a decimal format and stores it in de. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Floating point to integer casting
    ... Conversions in such cases are required to discard extra ... precision and range. ... when deciding when to simplify expressions. ... "The values of floating operands and of the results of floating ...
    (comp.lang.c)
  • Re: Floating point to integer casting
    ... Conversions in such cases are required to discard extra ... precision and range. ... when deciding when to simplify expressions. ... Some floating point hardware works internally using 80-bits, when the precision of double is 64-bits, which can lead to inconsistencies when intermediate 80-bit results are written to memory as 64-bits then loaded again, compared with keeping the intermediate values in the registers. ...
    (comp.lang.c)
  • Re: Field changed from Single to Double issue..
    ... >by hand as 0.3 so there is no precision issue with it. ... >>I think you're missing two zeros in the precision ... >conversions work. ... >>You could maybe do your conversions via a Recordset. ...
    (microsoft.public.access.conversion)
  • Re: Field changed from Single to Double issue..
    ... >I think you're missing two zeros in the precision there... ... >You could maybe do your conversions via a Recordset. ... truncation to control ...
    (microsoft.public.access.conversion)