This post is about calculating logarithms using Java. Java provides supports for logarithms via the java.lang.Math package, the log() function.This function is to the base e.Base e or the natural logarithm represent the log to a constant (e) whose value is approximately 2.718281828. To compute the logarithm you can use the code below

1 2 |
double input=10; double logvalue= Math.log(input); |

Now another question is how do you find the logarithm for other bases, example log to base 2 or log to base 10. This can be easily accomplished using the below property of logarithms.

1 |
log(x) base n = log (x) base e / log (n) base e |

Using this property we can change to any base. Let us take the example to understand this. How to find the log of 8 to the base 2.

log (8) to base 2 = log (8) base e / log (2) base e

1 2 3 |
//find log to base 2 of 8 double a=(Math.log(8)/Math.log(2)); System.out.print(a); |