[Python-Dev] [Python-checkins] r64424 - in python/trunk:Include/object.h Lib/test/test_sys.py Misc/NEWSObjects/intobject.c Objects/longobject.c Objects/typeobject.cPython/bltinmodule.c

"Martin v. Löwis" martin at v.loewis.de
Thu Jun 26 23:47:31 CEST 2008


Guido van Rossum wrote:
> On Thu, Jun 26, 2008 at 1:46 PM, Mark Dickinson <dickinsm at gmail.com> wrote:
>> I'd be delighted with '%a' support.
> 
> Remind me what %a does?

It's a C99 feature. From the spec (7.19.6.1p8)

       a,A     A  double  argument  representing  a  floating-point
               number is converted  in  the  style  [-]0xh.hhhhp±d,
               where  there  is  one  hexadecimal  digit  (which is
               nonzero if the argument is  a  normalized  floating-
               point  number  and  is otherwise unspecified) before
               the decimal-point character235) and  the  number  of
               hexadecimal   digits   after  it  is  equal  to  the
               precision; if the precision is missing and FLT_RADIX
               is  a  power  of 2, then the precision is sufficient
               for an exact representation of  the  value;  if  the
               precision is missing and FLT_RADIX is not a power of
               2,   then   the   precision   is    sufficient    to
               distinguish236) values of type double,  except  that
               trailing  zeros  may be omitted; if the precision is
               zero and the # flag is not  specified,  no  decimal-
               point  character  appears.   The  letters abcdef are
               used for a conversion and the letters ABCDEF  for  A
               conversion.   The  A conversion specifier produces a
               number with X  and  P  instead  of  x  and  p.   The
               exponent  always  contains  at  least one digit, and
               only as many more digits as necessary  to  represent
               the  decimal  exponent  of 2.  If the value is zero,
               the exponent is zero.

               A double argument representing an infinity or NaN is
               converted  in  the  style  of  an  f or F conversion
               specifier.

Footnotes
     235) Binary implementations can choose the  hexadecimal  digit
          to  the  left  of  the  decimal-point  character  so that
          subsequent digits align to nibble (4-bit) boundaries.

     236) The  precision  p  is sufficient to distinguish values of
          the source type if 16p-1>bn where b is FLT_RADIX and n is
          the  number  of  base-b  digits in the significand of the
          source type.  A smaller p might suffice depending on  the
          implementation's  scheme for determining the digit to the
          left of the decimal-point character.

This is symmetric with C99's hexadecimal floating point literals:

 hexadecimal-floating-constant:
   hexadecimal-prefix hexadecimal-fractional-constant
                     binary-exponent-part floating-suffix-opt
   hexadecimal-prefix hexadecimal-digit-sequence
                     binary-exponent-part floating-suffix-opt

 hexadecimal-fractional-constant:
   hexadecimal-digit-sequence-opt . hexadecimal-digit-sequence
   hexadecimal-digit-sequence .

 binary-exponent-part:
   p sign-opt digit-sequence
   P sign-opt digit-sequence

 hexadecimal-digit-sequence:
   hexadecimal-digit
   hexadecimal-digit-sequence hexadecimal-digit

scanf and strtod support the same format.

Regards,
Martin




More information about the Python-Dev mailing list