Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Printing a very small number

by Dr Manhattan (Beadle)
on Aug 16, 2013 at 10:13 UTC ( #1049718=perlquestion: print w/ replies, xml ) Need Help??
Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

How can I print a very small snumber, like 9^-10 or something?

At the moment I'm just getting output like '3e-009', which is not very usefull.

Thanks in advance for any help

Comment on Printing a very small number
Re: Printing a very small number
by McA (Curate) on Aug 16, 2013 at 10:18 UTC

    Have a look at printf.

    UPDATE:

    my $a = 0.0000000000007; printf("%.15f\n", $a);

    McA

Re: Printing a very small number
by daxim (Chaplain) on Aug 16, 2013 at 10:18 UTC
    $ 9 ** -10 2.86797199079244131e-10 $ sprintf '%0.94f', 9 ** -10 0.00000000028679719907924413134598230166468724084040268010320495761789 +06136192381381988525390625
Re: Printing a very small number (scientific e notation)
by Anonymous Monk on Aug 16, 2013 at 10:27 UTC

    How can I print a very small snumber, like 9^-10 or something?

    What number is that supposed to be?

    At the moment I'm just getting output like '3e-009', which is not very usefull

    That is http://en.wikipedia.org/wiki/Scientific_notation#E_notation, its useful it means 3 times ten to the power of negative nine

    $ perl -le " my $num = 3e-009; $num =~ s{[eE]([+-])?0+}{ * 10**$1}; pr +int $num; " 3 * 10**-9 $ perl -le " print 3 * 10**-9 " 3e-009
Re: Printing a very small number
by kcott (Abbot) on Aug 16, 2013 at 10:38 UTC

    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

      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

Re: Printing a very small number
by betterworld (Deacon) on Aug 16, 2013 at 11:37 UTC

    For a precise output:

    use Math::BigRat; my $num = Math::BigRat->new("9e-10"); print $num->as_float(), "\n";

    Output: 0.0000000009

      For a precise output:

      That works if your small number starts out as a string, but if it comes from a calculation done using normal Perl math, the 'damage' has already been done.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Printing a very small number
by Anonymous Monk on Aug 16, 2013 at 16:40 UTC
    If you're managing to get 3e-009 as output when you expect 9^-10, you've got bigger problems than output formatting.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-09-22 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (191 votes), past polls