FAQ
flags
[...]
space prefix non-negative number with a space
+ prefix non-negative number with a plus sign
[...]
When a space and a plus sign are given as the flags at
once, a plus sign is used to prefix a positive number.


After reading the above, I would expect

sprintf( "%+ d", 0 )

to return "0", but it returns "+0".


Is this a documentation or an implementation issue?

I of course prefer (my reading of) the documentation,
because it provides a way to have an unsigned 0
in a further fully signed environment.

--
Ruud

Search Discussions

  • Zefram at Oct 26, 2009 at 1:22 pm

    Dr.Ruud wrote:
    After reading the above, I would expect

    sprintf( "%+ d", 0 )

    to return "0", but it returns "+0".
    For your interpretation to be consistent, you'd have to expect that to
    return " 0", i.e., space prefixed rather than unprefixed.
    Is this a documentation or an implementation issue?
    The documentation is in error: the "positive" should have been
    "non-negative". Older versions of perlfunc(1) have "positive" instead of
    "non-negative" in the descriptions of the individual flags too. POSIX is
    clear (well, if you read it very carefully) that the flags affect zero:

    +
    The result of a signed conversion shall always begin with a sign
    ( '+' or '-' ). The conversion shall begin with a sign only when
    a negative value is converted if this flag is not specified.
    <space>
    If the first character of a signed conversion is not a sign or
    if a signed conversion results in no characters, a <space> shall
    be prefixed to the result. This means that if the <space> and
    '+' flags both appear, the <space> flag shall be ignored.

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.html

    -zefram
  • Dr.Ruud at Oct 26, 2009 at 6:39 pm

    Zefram wrote:
    Dr.Ruud wrote:
    After reading the above, I would expect

    sprintf( "%+ d", 0 )

    to return "0", but it returns "+0".
    For your interpretation to be consistent, you'd have to expect that to
    return " 0", i.e., space prefixed rather than unprefixed.
    Certainly not, see the doc:

    "When a space and a plus sign are given as the flags at
    once, a plus sign is used to prefix a positive number."

    (which means to me that the space is only functional, not displayable,
    much like an escape character)

    Is this a documentation or an implementation issue?
    The documentation is in error: the "positive" should have been
    "non-negative". Older versions of perlfunc(1) have "positive" instead of
    "non-negative" in the descriptions of the individual flags too. POSIX is
    clear (well, if you read it very carefully) that the flags affect zero:

    +
    The result of a signed conversion shall always begin with a sign
    ( '+' or '-' ). The conversion shall begin with a sign only when
    a negative value is converted if this flag is not specified.
    <space>
    If the first character of a signed conversion is not a sign or
    if a signed conversion results in no characters, a <space> shall
    be prefixed to the result. This means that if the <space> and
    '+' flags both appear, the <space> flag shall be ignored.

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.html
    Perl's printf is different.

    --
    Ruud (who should really just send in a patch that matches his reading)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl5-porters @
categoriesperl
postedOct 25, '09 at 10:42p
activeOct 26, '09 at 6:39p
posts3
users2
websiteperl.org

2 users in discussion

Dr.Ruud: 2 posts Zefram: 1 post

People

Translate

site design / logo © 2022 Grokbase