Java Math.log10() method returns base 10 logarithm of the double argument. In this tutorial, we will discuss log10() method with examples.
Syntax of Math.log10() method
Math.log10(10); //returns 1.0
log10() Description
public static double log10(double num): Returns the base 10 logarithm of double argument num
. The returns type of log10() method is double.
log10() Parameters
- num: The double value whose base 10 logarithm is to be determined.
log10() Return Value
- Returns base 10 logarithm of double value
num
. - If the value of
num
is NaN (Not a number) or less than zero then it returns NaN. - If
num
is positive infinity then it returns positive infinity. - If argument
num
is either positive zero or negative zero then it returns negative infinity. - If argument is 10n then the result n.
Example 1: Base 10 logarithm of number 2 and 10
public class JavaExample { public static void main(String[] args) { double num = 2, num2 = 10; System.out.println("Base 10 log of 2: "+Math.log10(num)); System.out.println("Base 10 log of 10: "+Math.log10(num2)); } }
Output:
Example 2: Log of 10n
public class JavaExample { public static void main(String[] args) { double num = 10000; //10 to the power 4 System.out.println(Math.log10(num)); } }
Output:
Example 3: Log10 of negative value or NaN
public class JavaExample { public static void main(String[] args) { double num = -2, num2 = 0.0/0; System.out.println("Log of -ve value: "+Math.log10(num)); System.out.println("Log of NaN: "+Math.log10(num2)); } }
Output:
Example 4: Log10 of zero or infinity
public class JavaExample { public static void main(String[] args) { double num = 0, num2 = 5.0/0; System.out.println("Log10 of zero: "+Math.log10(num)); System.out.println("Log10 of infinity: "+Math.log10(num2)); } }
Output: