Logarithm Methods


Logarithm Methods

The Math class from the java.lang package provides the log() method that returns the natural logarithm of the input argument. The natural logarithm is a very important mathematical function, but it is not the only useful logarithm. The base 10, or common, logarithm, is used for things like computing the acid/alkaline balance of a liquid or converting sound intensities to decibels. You may come across uses for logarithms of other base numbers as well.

The Math2 class defines two additional logarithm methods. The first, log10() , returns the base 10 logarithm of an input argument. The second, logX() , computes a logarithm using a user -specified base number. To determine an expression for an arbitrary base logarithm, we make use of the formula for converting logarithms from one base to another.

Equation 17.2

graphics/17equ02.gif


Eq. (17.2) shows a very interesting relation. It says that the logarithm of a number a to the base b is equal to the logarithm of a to the base z divided by the logarithm of b to the base z . The number z is arbitrary. If we set z equal to Euler's constant, z = e , we can make use of the built-in Math.log() method. The logarithm conversion expression now becomes

Equation 17.3

graphics/17equ03.gif


Eq. (17.3) will be used by the logX() method. The common logarithm uses a specific version of Eq. (17.3) where b = 10. In this case, the conversion expression becomes that shown in Eq. (17.4).

Equation 17.4

graphics/17equ04.gif


The code listings for the log10() and logX() methods are shown next . Both make use of the Math.log() method. A static constant named LN10 will be declared at the top of the Math2 class to be equal to ln(10). You will notice that the two logarithm methods are very simple. They consist of only one line, a return statement.

 public static double log10(double a) {   return Math.log(a)/LN10; } public static double logX(double a, double b) {   return Math.log(a)/Math.log(b); } 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net