Calculating a Logarithm

double logValue = Math.log(125.5);

In this phrase, we use the log() method of the java.lang.Math class to calculate the logarithm of the parameter passed in. We pass in a value of double type, and the return value is also a double value. The log() method calculates the natural logarithm, using base e, where e is the standard value of 2.71828.

JDK1.5 added a new method to the Math class for directly calculating a base 10 logarithm. This is the log10() method. Similar to the log() method, this method takes a double input parameter, and returns a double result type. We can use this method to easily calculate a base 10 logarithm as follows:

double logBase10 = Math.log10(200);

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.