The Java log Function is one of the Java Math Library function which is used to calculate the logarithmic value of given number with base E. In this article we will show you, How to write Math.log function in Java Programming language with example.
Java log Function syntax
The basic syntax of the Math.log in Java Programming language is as shown below. Following function will accept positive double value as argument and returns the logarithmic value of the specified expression or Value.
1 2 3 4 |
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, Math.log function will return the logarithm of a given value.
- If the number argument is negative double value, Math.log function will return NaN.
- If the number argument is not a number (NaN), java Math.log function will return NaN.
- If 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 you to find 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 CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// 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)); } } |
OUTPUT
ANALYSIS
First, We declared variable of type Double and performed the Math.log function directly on expression.
1 2 |
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.
1 2 |
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.
1 2 |
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 program we will show you, 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 each and every element in an array.
JAVA CODE
1 2 3 4 5 6 7 8 9 10 11 12 |
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])); } } } |
OUTPUT
ANALYSIS
First, We declared an Array of double type and assigned some random values.
1 |
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 condition is True, statement inside the for loop will be executed.
TIP: logArray.length is used to find the length of an array.
1 |
for (int i = 0; i < logArray.length; i++) { |
Following statements will print the output. If you observe the code snippet, we used the Math.log Function directly inside the System.out.format statement. Here, compiler will call the Java Math.log method ( static double log(double number) ) to find the logarithmic value of corresponding values.
1 |
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 are going to declare an arraylist of double type and find the logarithmic value of list elements.
JAVA CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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)); } } } |
OUTPUT
ANALYSIS
First, We declared an ArrayList of double type and assigned some random values.
1 2 3 4 5 6 7 |
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
1 |
for (double x : logList) { |
Following statements will print the output. If you observe the code snippet, we used the Math.log Function directly inside the System.out.format statement. Here, compiler will call the java Math.log method ( static double log(double x) ) to find the logarithmic value of corresponding values.
1 |
System.out.println("Logarithmic Value of ArrayList = " + Math.log(x)); |
Thank You for Visiting Our Blog
Share your Feedback, or Code!!