Exploring the log10() Method in Java: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-04 04:11:03   6301  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Description

This java tutorial shows how to use the log10(double a) method of Math class under java.lang package. This method returns the base 10 logarithm of a value specified as method argument.

Method Syntax

public static double log10(double a)

Method Returns

The log10(double a) method simply returns the base 10 logarithm of a in consideration of the following special cases:

  • If the argument is NaN or less than zero, then the result is NaN.
  • If the argument is positive infinity, then the result is positive infinity.
  • If the argument is positive zero or negative zero, then the result is negative infinity.
  • If the argument is equal to 10n for integer n, then the result is n.
    • Compatibility Version

      Requires Java 1.5 and up

      Exception

      N/A

      Java Code Example

      This java example source code demonstrates the use of log10(double a) method of Math class. Basically we just ask for user input and then we will get the logarithm base 10.

      Output

      
      import static java.lang.System.*;
      
      import java.util.Scanner;
      
      /*
       * This example source code demonstrates the use of
       * log10(double a) method of Math class
       * Get the logarithmic value at base 10 of the user input
       */
      
      public class MathLogarithmBase10 {
      
      	public static void main(String[] args) {
      		// ask for user input
      		out.print("Enter a value:");
      		Scanner scan = new Scanner(System.in);
      		// use scanner to get user console input
      		double value = scan.nextDouble();
      		// get the logarithm value base 10
      		double logValue = Math.log10(value);
      		out.println("logarithm of "+value+" = "+logValue);
      		// close the scanner object to avoid memory leak
      		scan.close();
      
      	}
      
      }
       

      Output

       Enter a value:10
      logarithm of 10.0 = 1.0
      Press any key to continue . . .
       

      Exception Scenario

      N/A

      Similar Method

      N/A