Open In App

Java Math.log10() Method

Last Updated : 12 May, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In Java, the Math.log10() method is a part of the java.lang.Math library. This method is used to calculate the base 10 logarithm of a given number. This method takes a double value as a parameter.

Behavior of Math.log10() method:

  • If the input value is positive, this method will return the logarithm of that value.
  • If the input value is NAN or a negative number, this method will return NaN.
  • If the input is positive infinity, this method will return the result as positive infinity.
  • If the input is zero, this method will return the result as negative infinity.

Syntax of Math.log10() Method

public static double log10(double a)

  • Parameter: The parameter a, which is a double value for which we calculate the base 10 logarithm.
  • Return Type: This method returns the base 10 logarithm of the provided value a.

Now, we are going to understand this method with an example for better understanding. We will see how we are going to handle different inputs.


Examples of Java Math.log10() Method

Example 1: Logarithm of a Power of 10

In this example, we are calculating the base 10 logarithm for various values including a power of 10, a positive number, a negative number, positive infinity, and zero.

Java
// Java program to demonstrate 
// the Math.log10() method
import java.lang.Math;

public class Geeks {
    
    public static void main(String[] args) {
         // A power of 10
        double a = 1000;    
        // A positive double
        double b = 145.256;  
        // A negative number
        double c = -6.04;  
        // Positive infinity
        double d = Double.POSITIVE_INFINITY; 
        // Zero
        double e = 0;                   

        // A power of 10 as input
        System.out.println(Math.log10(a)); 

        // A positive double as input
        System.out.println(Math.log10(b)); 

        // A negative number as input
        System.out.println(Math.log10(c)); 

        // Positive infinity as input
        System.out.println(Math.log10(d));  

        // Zero as input
        System.out.println(Math.log10(e)); 
    }
}

Output
3.0
2.1621340805671756
NaN
Infinity
-Infinity

Explanation:

  • Here, for a=1000, the logarithm of 1000 with base 10 is 3 therefore the output for this becomes 3.0.
  • For b=145.256, the base 10 logarithm of 145.256 is around 2.162 therefore the output for this will become 2.1621340805671756.
  • For c=-6.04 as we have discussed above the logarithm of a negative number is undefined, therefore for this the output will become NaN.
  • For d= Double.POSITIVE_INFINITY, the logarithm of a postive infinity is positive infinity and therefore the output for this will become infinity.
  • For e=0, the logarithm of zero is negative infinity therefore the output for this will become -infinity.

What if we want to calculate the logarithm of a very small number how we are going to do this. Let's understand this with an example.

Example 2: Logarithm of a Small Positive Value

In this example, we are calculating the base 10 logarithm of a small positive number to observe how the method handles very small inputs.

Java
// Java program to demonstrate 
// the Math.log10() method
import java.lang.Math;

public class Geeks {
    
    public static void main(String[] args) {
        
        // A small positive value
        double f = 0.001; 

        // Logarithm of a small positive number
        System.out.println(Math.log10(f)); 
    }
}

Output
-3.0

Explanation: Here, for f-0.001, the logarithm of 0.001 with base 10 is -3, therefore for this the output will become -3.0.

Important Points:

  • log10() method calculates the logarithm of a number with base 10.
  • This method can handle different type of inputs, such as positive, negative, and zero.

Next Article
Article Tags :
Practice Tags :

Similar Reads