Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: Printing a very small number

by Laurent_R (Canon)
on Aug 16, 2013 at 17:32 UTC ( #1049762=note: print w/replies, xml ) Need Help??

in reply to Re: Printing a very small number
in thread Printing a very small number

print sprintf "%.15f" => $x;

While this works perfectly, this is a bit verbose IMHO. Why not simply:

printf "%.15f" => $x;

or even:

printf "%.15f", $x;

Replies are listed 'Best First'.
Re^3: Printing a very small number
by kcott (Chancellor) on Aug 17, 2013 at 05:16 UTC

    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

      Well, thank you for your answer, Ken, I guess it is probably a matter of personal taste. Perhaps a reminiscence of my C background. However, as far as the gotchas are concerned, if you look at the one-liner in this post I made two days ago Re^2: How to calculate the sum of columns to be equal to 100?, you'll see that I did not fall in any of the two traps you're mentioning: I am using print, not printf, when I don't need specific formatting (it is actually extremely rare that I use printf), and I am adding a new line in the formatting when needed, nothing particularly complicated. As for the use of the fat comma (=>), I found it slightly distracting, I actually had to think a couple of seconds to understand its meaning in this context. But, again, maybe a prejudice due to C reminiscences on my part.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1049762]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-08-17 18:37 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (182 votes). Check out past polls.