The sprintf documentation contains details of the formats; these are not shown in the printf documentation. So, as it was the sprintf formatting that I was explaining, it seemed appropriate to use sprintf in my example code.

More importantly though, I made a conscious decision to not use printf in my examples. That function has a number of gotchas which aren't specifically related to the actual formatting and which I didn't want to have to explain.

The printf documentation is short: just three paragraphs. The first gotcha, noted in the very first sentence, has tripped you up!

Here's what happens if I substitute my 'print sprintf' with your ("Why not simply") 'printf':

$ perl -Mstrict -Mwarnings -le ' my $x = 9**-10; print "$x"; printf "%f" => $x; printf "%.15f" => $x; printf "%.31f" => $x; printf "%g" => $x; printf "%.15g" => $x; printf "%.31g" => $x; ' 2.86797199079244e-10 0.0000000.0000000002867970.00000000028679719907924413493062.86797e-102 +.86797199079244e-102.86797199079244134930566254989e-10$

I'll also draw your attention to the last printf paragraph:

"Don't fall into the trap of using a printf when a simple print would do. The print is more efficient and less error prone."

Finally, I use "=>" insted of "," for clarity; particularly when separating different types of arguments. If you look at my posts, you'll find many examples of this kind of thing:

sprintf FORMAT => LIST split PATTERN => STRING join STRING => LIST pack TEMPLATE => LIST

I find it makes the code easier to read and, when necessary, easier to debug. There's a clear delineation between the first argument, which affects how the function operates, and the remaining arguments, which specify what the function operates on.

-- Ken


In reply to Re^3: Printing a very small number by kcott
in thread Printing a very small number by Dr Manhattan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.