The Java log Function is one of the Java Math Library functions used to calculate the logarithmic value of the given number with base E.
Java log Function syntax
The basic syntax of the Math.log in Java Programming language 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 or Value.
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 value or a valid numerical expression for which you want to find the logarithmic value.
- If the number argument is positive double value, Java Math.log function will return the logarithm of a given value.
- If the number argument is negative double value or not a number (NaN), the java Math.log function will return NaN.
- When the number argument is positive infinity, Math.log function will return the result as Positive Infinity. If the number argument is positive or negative zero, Math.log function will return the result as Negative Infinity
Java log Function Example
The Java 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.
// Java Math.log Function 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)); } }
First, We declared a variable of type Double and performed the Math.log function directly on expression.
double a = Math.log(10.90 + 15.10 - 22.50 + 1.50); System.out.println("Logarithmic Value = " + a);
Next, We used the Math.log Function directly on Positive double values.
System.out.println("\nLogarithmic Value of Positive Number: " + Math.log(1)); System.out.println("Logarithmic Value of Positive Number: " + Math.log(10.95));
Next, We used the java Math.log 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.log(-8.70)); System.out.println("Logarithmic Value of Negative Number: " + Math.log(-5.00));
Java log on Array example
In this Java log 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("Logarithmic Value of Array Element = %.4f\n", Math.log(logArray[i])); } } }
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 Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, compiler will check for the condition (i < logArray.length). As along the log condition is True, the 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 statements will print the output. If you observe the code snippet, we used the log Math function directly inside the System.out.format statement. Here, the Jcompiler will call the Java Math.log method ( static double log(double number) ) to find the logarithmic value of corresponding values.
System.out.format("Logarithmic Value of Array Element = %.4f\n", Math.log(logArray[i]));
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 Java program, we declare an arraylist of double type and find the logarithmic value of list elements using Java log.
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("Logarithmic Value of ArrayList = " + Math.log(x)); } } }
First, We declared an ArrayList of double type and assigned some random values.
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);
Next, We used the For Loop to iterate the double values in ArrayList
for (double x : logList) {
The below Java statement prints the output of a log function. Here, the compiler will call the java Math.log method ( static double log(double x) ) to find the logarithmic value of corresponding values.
System.out.println("Logarithmic Value of ArrayList = " + Math.log(x));