Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Printing a very small number

by kcott (Chancellor)
on Aug 16, 2013 at 10:38 UTC ( #1049724=note: print w/replies, xml ) Need Help??


in reply to Printing a very small number

G'day Dr Manhattan,

Saying "is not very useful" is not very useful to us. What format would you find useful? sprintf may do what you want; its documentation has copious examples. Here's a few possibilities:

$ perl -Mstrict -Mwarnings -le ' my $x = 9**-10; print "$x"; print sprintf "%f" => $x; print sprintf "%.15f" => $x; print sprintf "%.31f" => $x; print sprintf "%g" => $x; print sprintf "%.15g" => $x; print sprintf "%.31g" => $x; ' 2.86797199079244e-10 0.000000 0.000000000286797 0.0000000002867971990792441349306 2.86797e-10 2.86797199079244e-10 2.86797199079244134930566254989e-10

-- Ken

Replies are listed 'Best First'.
Re^2: Printing a very small number
by Laurent_R (Canon) on Aug 16, 2013 at 17:32 UTC

    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;

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2018-04-21 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?