Java logarithmic functions and Java logarithm operations2010-05-17 10:32 China it lab anonymous Keyword: Java
Java logarithmic functions are computationally problematic, but there are amazing errors in the API. But if you use the following method, the Java processing of the number of small problems encountered can be easily solved.
Sun's J2SE provides a single Java logarithm method--double Java.lang.Math.log (double), which is easy to use. Take a look at the following code:
Double x = Math.log (5);
Equivalent to: x = ln 5 or x = Loge5, which is the natural logarithm of base e.
If you want to use Java to calculate the logarithm of the computer, how to do the different logarithm of the bottom? Unfortunately, we have not yet been able to calculate the logarithm of base 10 or base 2. But they are the most used when calculating the Java logarithm. To solve this problem, you need to use mathematical and logarithmic equations:
LOGX (y) =loge (x)/Loge (y), bottom-changing formula
This requires a simple Java program to implement the logarithmic operation:
Package Com.generationjava.math;
public class Logarithm {
static public double log (double value, double base) {
return Math.log (value)/Math.log (base);
}
}
Using the Java logarithmic function to calculate the base 10 logarithm of 100 becomes very simple:
Double log = Logarithm.log (100, 10); Log is 2.0
The base 2 logarithm of 512 is:
Double log = Logarithm.log (512, 2); Log is 9.0
The following two simple Java logarithm operations are also useful:
static public double log2 (double value) {
Return log (value, 2.0);
}
static public double log10 (double value) {
Return log (value, 10.0);
Java logarithmic functions and Java logarithm operations