develooper Front page | perl.perl5.porters | Postings from June 2015

[perl #125471] sprintf behavior or documentation bug

Thread Next
From:
Tony Cook via RT
Date:
June 25, 2015 04:26
Subject:
[perl #125471] sprintf behavior or documentation bug
Message ID:
rt-4.0.18-14828-1435206375-1155.125471-15-0@perl.org
On Tue Jun 23 19:46:09 2015, saint.snit@gmail.com wrote:
> This is a bug report for perl from saint.snit@gmail.com,
> generated with the help of perlbug 1.40 running under perl 5.20.2.
> 
> Hi,
> 
> I've found a discrepancy between documentation and behavior.  I'm not
> sure which is correct.
> 
> perldoc -f sprintf says, "When a space and a plus sign are given as
> the flags at once, a plus sign is used to prefix a positive number."
> This is not accurate.  As the documentation notes,
> 
> printf '<%+ d>', 12;   # prints "<+12>"
> 
> However, the plus sign is printed even in the case of one number that
> isn't positive:
> 
> printf '<%+ d>', 0;   # prints "<+0>" instead of "<0>" or "< 0>"
> 
> Is the documentation supposed to say "a plus sign is used to prefix a
> nonnegative number"?  Or is the combination of space and + supposed to
> inhibit the printing of a plus sign before a 0?  The latter makes more
> sense, since there seems to be no other way to inhibit the illogical
> "+0" output while retaining the + for positive numbers.  (Why isn't
> this
> simply the default for all uses of the + flag?)

I expect it's meant to ignore the space flag if the + flag is present, perl's sprintf() formatting mostly follows the C standard where applicable, which states:

  If the space and + flags both appear, the space flag is ignored.

So it looks like:

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

should be:

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

Tony


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=125471

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About