PHP Numbers Taking Logarithms - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Numbers Taking Logarithms - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Thursday, May 9, 2019

PHP Numbers Taking Logarithms

PHP Numbers




Taking Logarithms

Problem 

You want to take the logarithm of a number.

Solution

For logs using base e (natural log), use log():

            // $log is about 2.30258
            $log = log(10);

For logs using base 10, use log10():

            // $log10 == 1
            $log10 = log10(10);

For logs using other bases, pass the base as the second argument to log():

            // log base 2 of 10 is about 3.32
            $log2 = log(10, 2);


Discussion

Both log() and log10() are defined only for numbers that are greater than zero. If you pass in a number equal to or less than zero, they return NAN, which stands for not a number.


No comments:

Post a Comment

Post Top Ad