Java log Function

The Java log Function is one of the Math Library functions used to calculate the logarithmic value of the given number with base E.

The basic syntax of the Java Math.log function is as shown below. The following function will accept a positive double value as an argument and returns the logarithmic value of the specified expression.

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

// In order to use in program: 
Math.log(double number);

Number: It can be a double or a valid numerical expression for which you want to find the logarithmic value.

  • If the number argument is a positive double value, the Java Math.log function will return the logarithm.
  • If the number argument is negative double or not a number (NaN), the Math.log function will return NaN.
  • When the number argument is positive infinity, it will return the result as Positive Infinity. If the number argument is positive or negative zero, it will return the result as Negative Infinity.

Java math log Function Example

The Math.log Function allows finding the logarithmic value of a specified expression or an individual double value. In this Java program, We are going to find the same and display the output.

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

For the next two lines, we used the Java Math.log Function directly on Positive double values.

In the last two lines, we used the log Function directly on Negative double values. As you can see from the screenshot, it is returning NaN for negative values.

// Example
package MathFunctions;

public class LogMethod {
	public static void main(String[] args) {
		double a = Math.log(10.90 + 15.10 - 22.50 + 1.50);
		System.out.println("Logarithmic Value = " + a);	

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

Java math log function on Array

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

package MathFunctions;

public class LogMethodOnArrays {
	public static void main(String[] args) {
		
		double [] logArray = {2.35, 4.98, -8.41, -9.5987, -0, 12.38, 14.4897};

		for (int i = 0; i < logArray.length; i++) {
			System.out.format("%.4f\n", Math.log(logArray[i]));
		}
	}
}
0.8544
1.6054
NaN
NaN
-Infinity
2.5161
2.6734

First, We declared an Array of double type and assigned some random values.

double [] logArray = {2.35, 4.98, -8.41, -9.5987, -0, 12.38, 14.4897};

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

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

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

The following System.out.format statements will print the output. If you observe the code snippet, we used the Math function directly inside the System.out.format statement. Here, the Javac will call the Math.log method ( static double log(double number) ) to find the logarithmic value of corresponding ones.

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

Java log function on Arraylist example

In this program, we declare an Arraylist of double type and find the logarithmic value of list elements using the log function.

First, We declared an ArrayList of double type and assigned some random values. Next, We used the For Loop to iterate the double values in ArrayList.

The below Java System.out.println statement prints the output. Here, the compiler will call the Math.log method ( static double log(double x) ) to find the logarithmic value of corresponding numbers.

package MathFunctions;

import java.util.ArrayList;

public class LogMethodOnArrayList {
	public static void main(String[] args) {
		
		ArrayList<Double> logList = new ArrayList<Double>(5);
		logList.add(12.998);
		logList.add(-1480.05);
		logList.add(0.876);
		logList.add(0.01);
		logList.add(20.98);
		logList.add(-8.89);
		
		for (double x : logList) {
			System.out.println(Math.log(x));
		}
	}
}
2.564795499472157
NaN
-0.13238918804574562
-4.605170185988091
3.043569602968151
NaN