%a format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Lenton

    #1

    %a format

    Is there any reason python's printf-style formatting is missing the
    (C99) '%a' specifier?

    I'm sorry if this has been asked and answered before; I can't find it
    on google ('%a' is a very awkward thing to look for):

    --
    John Lenton (john@grulic.or g.ar) -- Random fortune:
    Los cementerios están llenos de valientes.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.5 (GNU/Linux)

    iD4DBQFBPf4FgPq u395ykGsRAundAJ Y6K3b3hoSl0Of1r 6KYpH4jYtYvAJ99 KydK
    2TNjiXJFj+rtikh Ru0c9DA==
    =T89S
    -----END PGP SIGNATURE-----

  • Tor Iver Wilhelmsen

    #2
    Re: %a format

    John Lenton <john@grulic.or g.ar> writes:
    [color=blue]
    > Is there any reason python's printf-style formatting is missing the
    > (C99) '%a' specifier?[/color]

    Because it's not very well known or used? A google for "C printf
    format string" gave lots of resources, none of which had %a.

    Java's JDK 1.5 java.util.Forma tter class supports it, though.

    Comment

    • Erik Max Francis

      #3
      Re: %a format

      Tor Iver Wilhelmsen wrote:
      [color=blue]
      > John Lenton <john@grulic.or g.ar> writes:
      >[color=green]
      > > Is there any reason python's printf-style formatting is missing the
      > > (C99) '%a' specifier?[/color]
      >
      > Because it's not very well known or used? A google for "C printf
      > format string" gave lots of resources, none of which had %a.[/color]

      It's not C99, anyway; my copy of C99 makes no mention of it (the only
      mention of %a is associated with gmtime.

      --
      __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
      / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
      \__/ Education is a state-controlled manufactory of echoes.
      -- Norman Douglas

      Comment

      • John Lenton

        #4
        Re: %a format

        On Tue, Sep 07, 2004 at 12:21:49PM -0700, Erik Max Francis wrote:[color=blue]
        > Tor Iver Wilhelmsen wrote:
        > [color=green]
        > > John Lenton <john@grulic.or g.ar> writes:
        > > [color=darkred]
        > > > Is there any reason python's printf-style formatting is missing the
        > > > (C99) '%a' specifier?[/color]
        > >
        > > Because it's not very well known or used? A google for "C printf
        > > format string" gave lots of resources, none of which had %a.[/color]
        >
        > It's not C99, anyway; my copy of C99 makes no mention of it (the only
        > mention of %a is associated with gmtime.[/color]

        hmm! weird. From printf(3):

        a,A (C99; not in SUSv2) For a conversion, the double
        argument is converted to hexadecimal notation (using the
        letters abcdef) in the style [‐]0xh.hhhhp±d; forA
        conversion the prefix 0X, the letters ABCDEF, and the
        exponent separator P is used. There is one hexadecimal
        digit before the decimal point, and the number of digits
        after it is equal to the precision. The default
        precision suffices for an exact representation of the
        value if an exact representation in base 2 exists and
        otherwise is sufficiently large to distinguish values of
        type double. The digit before the decimal point is
        unspecified for non‐normalize d numbers, and nonzero but
        otherwise unspecified for normalized numbers.

        and looking at ISO/IEC 9899:1999 (E), it's on page 278, §7.19.6.1:


        a,A

        A double argument representing a floating-point number is
        converted in the style [-]0xh.hhhh p±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 decimalpoint
        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.


        --
        John Lenton (john@grulic.or g.ar) -- Random fortune:
        It seems intuitively obvious to me, which means that it might be wrong.
        -- Chris Torek

        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.5 (GNU/Linux)

        iD8DBQFBPifjgPq u395ykGsRAiC9AK DHxzMZlIBagfhfd 8x47MjMHQYjkwCe IeYY
        Sb9juru++yV4/Oc0cAmyvus=
        =DrCR
        -----END PGP SIGNATURE-----

        Comment

        • Erik Max Francis

          #5
          Re: %a format

          John Lenton wrote:
          [color=blue]
          > and looking at ISO/IEC 9899:1999 (E), it's on page 278, §7.19.6.1:[/color]

          My bad, I did an incomplete search. Sorry about that.

          --
          __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
          \__/ I always entertain great hopes.
          -- Robert Frost

          Comment

          • Dan Bishop

            #6
            Re: %a format

            Tor Iver Wilhelmsen <tor.iver.wilhe lmsen@broadpark .no> wrote in message news:<usm9tx5n3 .fsf@broadpark. no>...[color=blue]
            > John Lenton <john@grulic.or g.ar> writes:
            >[color=green]
            > > Is there any reason python's printf-style formatting is missing the
            > > (C99) '%a' specifier?[/color]
            >
            > Because it's not very well known or used? A google for "C printf
            > format string" gave lots of resources, none of which had %a.[/color]

            They do if you search for "C99" instead of just "C".

            Anyhow, another reason for not supporting "%a" is that it would be
            redundant. It would be sufficent to define an "%x" format for floats.

            Comment

            • John Lenton

              #7
              Re: %a format

              On Tue, Sep 07, 2004 at 04:48:25PM -0700, Dan Bishop wrote:[color=blue]
              > Tor Iver Wilhelmsen <tor.iver.wilhe lmsen@broadpark .no> wrote in message news:<usm9tx5n3 .fsf@broadpark. no>...[color=green]
              > > John Lenton <john@grulic.or g.ar> writes:
              > > [color=darkred]
              > > > Is there any reason python's printf-style formatting is missing the
              > > > (C99) '%a' specifier?[/color]
              > >
              > > Because it's not very well known or used? A google for "C printf
              > > format string" gave lots of resources, none of which had %a.[/color]
              >
              > They do if you search for "C99" instead of just "C".
              >
              > Anyhow, another reason for not supporting "%a" is that it would be
              > redundant. It would be sufficent to define an "%x" format for floats.[/color]

              I don't think that's a good idea: no other specifier is dual
              integer/float, and you'd break code that passes floats to a %x
              expecting it to work as a %u, but in hex.

              Actually the answer I was expecting was "FooOS doesn't support it" or
              somesuch, not 'uhhh... el qué?' :) %a is very useful for what it was
              created: serializing floats without loss of precision, and I was
              surprised that python didn't support it.

              --
              John Lenton (john@grulic.or g.ar) -- Random fortune:
              De los parientes y el sol, entre más lejos, mejor.

              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.2.5 (GNU/Linux)

              iD8DBQFBPlB0gPq u395ykGsRAkbNAJ 9uBHuwo7xzm45Lt GuFnIknTsl5bQCf dQO8
              Xlr3B9uOJglR8D7 4sfTJt8I=
              =/+Dw
              -----END PGP SIGNATURE-----

              Comment

              • John Roth

                #8
                Re: %a format


                "Tor Iver Wilhelmsen" <tor.iver.wilhe lmsen@broadpark .no> wrote in message
                news:usm9tx5n3. fsf@broadpark.n o...[color=blue]
                > John Lenton <john@grulic.or g.ar> writes:
                >[color=green]
                >> Is there any reason python's printf-style formatting is missing the
                >> (C99) '%a' specifier?[/color]
                >
                > Because it's not very well known or used? A google for "C printf
                > format string" gave lots of resources, none of which had %a.
                >
                > Java's JDK 1.5 java.util.Forma tter class supports it, though.[/color]

                I suspect there's no major reason more significant than that
                no one has written the patch to support it. That might suggest
                that no one really cares enough to do so - IIRC, Python
                shifted from using the C library printf() function to its own
                code some time ago, so there's no real commitment to
                following the C standard in this instance.

                If the OP wants it, I would suggest that he write the patch
                (complete with the doc changes, etc.) and submit it. It might
                be a good idea to provide a use case more significant than
                "it's in the C99 standard" as well - but then I'm not
                channeling the core developers that well these days.

                John Roth


                Comment

                • Dan Bishop

                  #9
                  Re: %a format

                  "John Roth" <newsgroups@jhr othjr.com> wrote in message news:<10jsqrm2r sqdc39@news.sup ernews.com>...[color=blue]
                  > "Tor Iver Wilhelmsen" <tor.iver.wilhe lmsen@broadpark .no> wrote in message
                  > news:usm9tx5n3. fsf@broadpark.n o...[color=green]
                  > > John Lenton <john@grulic.or g.ar> writes:
                  > >[color=darkred]
                  > >> Is there any reason python's printf-style formatting is missing the
                  > >> (C99) '%a' specifier?[/color]
                  > >
                  > > Because it's not very well known or used? A google for "C printf
                  > > format string" gave lots of resources, none of which had %a.
                  > >
                  > > Java's JDK 1.5 java.util.Forma tter class supports it, though.[/color]
                  >
                  > I suspect there's no major reason more significant than that
                  > no one has written the patch to support it. That might suggest
                  > that no one really cares enough to do so - IIRC, Python
                  > shifted from using the C library printf() function to its own
                  > code some time ago, so there's no real commitment to
                  > following the C standard in this instance.
                  >
                  > If the OP wants it, I would suggest that he write the patch
                  > (complete with the doc changes, etc.) and submit it. It might
                  > be a good idea to provide a use case more significant than
                  > "it's in the C99 standard" as well - but then I'm not
                  > channeling the core developers that well these days.[/color]

                  Why stick to just adding one conversion specifier to one data type?
                  Why not allow a programmer to define their own custom specifiers?

                  class Bits(int):
                  def __sprintf__(sel f, flags, precision, conversion):
                  if conversion == 'b':
                  # return a binary string
                  return itoa(self, 2) # Why isn't this a standard function?
                  else:
                  return NotImplemented
                  [color=blue][color=green][color=darkred]
                  >>> n = Bits(100)
                  >>> # The % operator would call __sprintf__
                  >>> print '%b' % n[/color][/color][/color]
                  1100100[color=blue][color=green][color=darkred]
                  >>> # If it isn't implemented, use the current behavior as a default.
                  >>> print '%d' % n[/color][/color][/color]
                  100

                  You'd be able to redefine the existing conversion too, in case you
                  want, for example, a high-precision numeric type to convert
                  '%.1000f'%x accurately.

                  Comment

                  • Steven Bethard

                    #10
                    itoa (WAS: Re: %a format)

                    > class Bits(int):[color=blue]
                    > def __sprintf__(sel f, flags, precision, conversion):
                    > if conversion == 'b':
                    > # return a binary string
                    > return itoa(self, 2) # Why isn't this a standard function?
                    > else:
                    > return NotImplemented[/color]

                    Well, I'm guessing itoa isn't a standard function because it's not that
                    commonly used... Not that I'm claiming to have written the widest variety of
                    Python code, but I've never needed it.

                    It is available though through a relatively short sequence of builtins though:
                    [color=blue][color=green][color=darkred]
                    >>> def itoa(i, base):[/color][/color][/color]
                    .... return str(int(str(i), base))
                    ....[color=blue][color=green][color=darkred]
                    >>> itoa(100, 2)[/color][/color][/color]
                    '4'

                    A little roundabout, but it does do the trick.

                    Steve

                    Comment

                    • Steven Bethard

                      #11
                      Re: itoa (WAS: Re: %a format)

                      I wrote:
                      [color=blue][color=green][color=darkred]
                      > >>> def itoa(i, base):[/color][/color]
                      > ... return str(int(str(i), base))
                      > ...[/color]

                      Oops. My bad. You wanted the inverse. Just ignore me. ;)

                      Comment

                      • Erik Max Francis

                        #12
                        Re: itoa (WAS: Re: %a format)

                        Steven Bethard wrote:
                        [color=blue]
                        > Well, I'm guessing itoa isn't a standard function because it's not
                        > that
                        > commonly used...[/color]

                        It was a common library add-on in the DOS world, but it was never
                        standard.

                        --
                        __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                        / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
                        \__/ Never had very much to say / Laugh last, laugh longest
                        -- Des'ree

                        Comment

                        Working...