Java log10 Function

Java log10 is a Math Library function, used to return the base 10 logarithmic value of a given number. The syntax of Math.log10 in Java accepts positive double value & returns the base 10 logarithmic value.

static double log10(double number); //Return Type is Double

// To use in program: 
Math.log10(double number);

Java log10 Function Example

In this Java Math.log10 program, we are going to find log10 values for different data.

// Java Math.log10 Function
package MathFunctions;

public class Log10Method {
 public static void main(String[] args) {
 double a = Math.log10(100.90 + 150.10 - 220.50 + 10.50);
 System.out.println("Logarithmic Value = " + a); 

 System.out.println("\nLogarithmic Value of Zero: " + Math.log10(0));
 
 System.out.println("\nLogarithmic Value of Positive Number: " + Math.log10(1));
 System.out.println("Logarithmic Value of Positive Number: " + Math.log10(10.95));
 
 System.out.println("\nLogarithmic Value of Negative Number: " + Math.log10(-8.70));
 System.out.println("Logarithmic Value of Negative Number: " + Math.log10(-5.00)); 
 }
}
Java log10 Function 1

First, We declared a variable of type Double and performed the Math.log10 function directly on expression.

double a = Math.log10(100.90 + 150.10 - 220.50 + 10.50);
System.out.println("Logarithmic Value = " + a);

Next, We used the log10 Math function directly on Positive double values.

System.out.println("\nLogarithmic Value of Positive Number: " + Math.log10(1));
System.out.println("Logarithmic Value of Positive Number: " + Math.log10(10.95));

Next, We used the java Math.log10 Function directly on Negative double values. As you can see from the screenshot, it is returning NaN for negative values.

System.out.println("\nLogarithmic Value of Negative Number: " + Math.log10(-8.70));
System.out.println("Logarithmic Value of Negative Number: " + Math.log10(-5.00));

Java log10 on Array example

In this Java program, we show how to find the base 10 logarithmic value of bulk data. Here, we are going to declare an Array of double type and find the base 10 logarithmic value of every element in an array.

package MathFunctions;

public class Log10MethodOnArrays {
	public static void main(String[] args) {
		
		double [] logArray = {20.35, 54.98, -68.41, -9.5987, -0, 120.38, 14.4897};

		for (int i = 0; i < logArray.length; i++) {
			System.out.format("Logarithmic Value of Array Element = %.4f\n", Math.log10(logArray[i]));
		}
	}
}
Logarithmic Value of Array Element = 1.3086
Logarithmic Value of Array Element = 1.7402
Logarithmic Value of Array Element = NaN
Logarithmic Value of Array Element = NaN
Logarithmic Value of Array Element = -Infinity
Logarithmic Value of Array Element = 2.0806
Logarithmic Value of Array Element = 1.1611

Within this log10 function example, First, We declared an Array of double type and assigned some random values.

double [] logArray = {20.35, 54.98, -68.41, -9.5987, -0, 120.38, 14.4897};

Next, We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < logArray.length). As along the loop condition is True, statement inside the for loop executed.

TIP: logArray.length is to find the length of an array.

for (int i = 0; i < logArray.length; i++) {

The following Java statements will print the output. If you observe the code snippet, we used the Java log10 Function directly inside the System.out.format statement. Here, the Jcompiler will call the Math.log10 method (static double log10(double number)) to find the base 10 logarithmic value of corresponding values.

System.out.format("Logarithmic Value of Array Element = %.4f\n", Math.log10(logArray[i]));

NOTE: If you want to find the base 10 logarithmic value of a single item, then use: Math.log10(myArray[index_position])

Java log10 function on Arraylist example

In this log10 program, we are going to declare an ArrayList of double type and find the base 10 logarithmic value of list elements.

package MathFunctions;

import java.util.ArrayList;

public class Log10MethodOnArrayList {
	public static void main(String[] args) {
		
		ArrayList<Double> logList = new ArrayList<Double>(5);
		logList.add(1245.998);
		logList.add(-1480.05);
		logList.add(250.9876);
		logList.add(170.01);
		logList.add(2016.98);
		logList.add(-8978.1898);
		
		for (double x : logList) {
			System.out.println("Logarithmic Value of ArrayList =  " + Math.log10(x));
		}
	}
}
Logarithmic Value of ArrayList =  3.0955173452206943
Logarithmic Value of ArrayList =  NaN
Logarithmic Value of ArrayList =  2.399652265765492
Logarithmic Value of ArrayList =  2.2304744673611583
Logarithmic Value of ArrayList =  3.304701591850494
Logarithmic Value of ArrayList =  NaN

Within this Java log10 function example, First, We declared an ArrayList of double type and assigned some random values.

ArrayList<Double> logList = new ArrayList<Double>(5);
logList.add(1245.998);
logList.add(-1480.05);
logList.add(250.9876);
logList.add(170.01);
logList.add(2016.98);
logList.add(-8978.1898);

Next, We used the For Loop to iterate the double values in ArrayList

for (double x : logList) {

The following statements will print the output. Here, the Jcompiler will call the Math.log10 method ( static double log10(double x) ) to find the base 10 logarithmic value of corresponding values.

System.out.println("Logarithmic Value of ArrayList =  " + Math.log10(x));