Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Sign bug in Number::Format

by Anonymous Monk
on Jun 05, 2012 at 20:00 UTC ( #974538=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is this a bug? Looks like a bug, but wanted to confirm here before I go and report a bug...
DB<1> use Number::Format; DB<2> print Number::Format::format_number(-0.00004,5) 4e-05

Comment on Sign bug in Number::Format
Download Code
Re: Sign bug in Number::Format
by herveus (Parson) on Jun 05, 2012 at 20:13 UTC
    Howdy!

    What were you expecting to get? A quick glance at the module documentation suggests that the output is what should have come out.

    yours,
    Michael
      He would expect a negative number. It does look like a bug.
Re: Sign bug in Number::Format
by toolic (Bishop) on Jun 05, 2012 at 20:23 UTC
    The POD for Number::Format mentions a format_negative function. I don't think format_number handles negative numbers, by design.

    UPDATE: Nevermind... this prints a negative value:

    print Number::Format::format_number(-0.0004,5)
Re: Sign bug in Number::Format
by ww (Bishop) on Jun 05, 2012 at 20:54 UTC
    Bug? It looks correct to me:
    4 x 10^-5 = 0.00004
    and 0.00004 = 4 x 10^-5 or 4e-05
    Ooops (ikegami & toolic bring me to my senses):
    DB<3> print Number::Format::format_number(-0.0004,5)
    -0.0004

    Peripheral note: strikeout makes "=" look like -.

Re: Sign bug in Number::Format
by Anonymous Monk on Jun 06, 2012 at 01:46 UTC
Re: Sign bug in Number::Format (Devel::DumpTrace)
by toolic (Bishop) on Jun 10, 2012 at 00:35 UTC
    I believe Devel::DumpTrace quickly identifies the root cause of the problem.
    perl -d:DumpTrace=+Number::Format -MNumber::Format -E 'say Number::For +mat::format_number(-0.00004, 5)' >>> -e:1:[__top__]: say Number::Format::format_number(-0.00004, 5 +) >>> Number/Format.pm:316:[_get_self]: unless (ref $_:(-4e-05,5)[0 +] && UNIVERSAL::isa($_:(-4e-05,5)[0], "Number::Format"))

    It seems that perl immediately interprets the value passed to the function (-0.00004) as scientific notation inside the function (-4e-05). A diff of this output vs. the output of a passing case with one less zero (-0.0004) shows where in the code the 2 cases diverge. I uploaded a patch to the open bug report.

    It then occurred to me to take a look a the coverage results (cpancover.com: new web site for CPAN code coverage). Sure enough, the line of code I patched was not covered by the test suite. This shows the value in using coverage to determine test quality.

    Morals of this story:

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://974538]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2015-07-04 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls