Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Get logarithm of base 10

by tamaguchi (Pilgrim)
on Feb 28, 2006 at 12:46 UTC ( [id://533328]=perlquestion: print w/replies, xml ) Need Help??

tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

Hello all As I know it natural logarithm of a number "$number" is written in perl as: log($numer) but how is it possible to get the logarithm with base 10 or other logartms such as with base 2 etc. Thank you for any help.

2006-02-28 Retitled by jdporter, as per Monastery guidelines
Original title: 'Logarithms'

Replies are listed 'Best First'.
Re: Get logarithm of base 10
by dempa (Friar) on Feb 28, 2006 at 12:51 UTC
    From "perldoc -f log":

    To get the log of another base, use basic algebra: The base-N log of a number is equal to the natural log of that number divided by the natural log of N. For example:
    sub log10 { my $n = shift; return log($n)/log(10); }

    -- 
    dempa

Re: Get logarithm of base 10
by northwind (Hermit) on Feb 28, 2006 at 13:00 UTC

    Or if you wanted base N (where N could be 2, π, 10, 16, etc.)...

    sub log_N { my $num = shift; my $base = shift; return log($num)/log($base); }

Re: Get logarithm to base 10
by ChemBoy (Priest) on Feb 28, 2006 at 19:25 UTC

    As an alternative to the techniques already mentioned, you could always use POSIX 'log10'; (assuming that's not too easy ;-).



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2025-03-21 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (63 votes). Check out past polls.