Join our newsletter for the latest updates.

Java Math log10()

The Java Math log10() method computes the base 10 logarithm of the specified value and returns it.

The syntax of the log10() method is:

Math.log10(double x)

Here, log10() is a static method. Hence, we are calling the method directly using the class name Math.


log10() Parameters

  • x - the value whose logarithm is to be computed

log10() Return Values

  • returns the base 10 logarithm of x
  • returns NaN if x is NaN or less than zero
  • returns positive infinity if x is positive infinity
  • returns negative infinity if x is zero

Note: The value of log10(10n) = n, where n is an integer.


Example: Java Math.log10()

class Main {
  public static void main(String[] args) {

    // compute log10() for double value
    System.out.println(Math.log10(9.0));       // 0.9542425094393249

    // compute log10() for zero
    System.out.println(Math.log10(0.0));       // -Infinity

    // compute log10() for NaN
    double nanValue = Math.sqrt(-5.0);
    System.out.println(Math.log10(nanValue));  // NaN

    // compute log10() for infinity
    double infinity = Double.POSITIVE_INFINITY;
    System.out.println(Math.log10(infinity));  // Infinity

    // compute log10() for negative numbers
    System.out.println(Math.log(-9.0));      // NaN

    //compute log10() for 103
    System.out.println(Math.log10(Math.pow(10, 3)));  // 3.0

  }
}

In the above example, notice the expression,

Math.log10(Math.pow(10, 3))

Here, Math.pow(10, 3) returns 103. To learn more, visit Java Math.pow().


Recommended Tutorial