Java log1p Function

The Java log1p Function is one of the Java Math Library functions, which is to return the natural logarithm of the sum of the arguments and 1. For the small values, the result of log1p(a) is much closer to the true result of ln(1 + a) than the floating-point evaluation of log(1.0 + a).

Java log1p Function syntax

The syntax of the Math.log1p in Java Programming language is as shown below. The following function accepts a positive double value as an argument. It returns the logarithmic value of the specified expression or Value.

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

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

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

  • If the number argument is a positive double value, Math.log1p function returns the natural logarithm of a given value.
  • If the number argument is a negative double value or not a number, Math.log1p function returns NaN.
  • When the number argument is positive infinity, Math.log1p function returns the result as Positive Infinity
  • If it is negative infinity, Math.log1p function returns the result as Negative Infinity
  • If it is positive or negative zero, Java Math.log1p function returns the result as Zero

Java log1p Function Example

The Java Math.log1p Function allows you to find the natural logarithm 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.log1p Function
package MathFunctions;

public class Log1pMethod {
	public static void main(String[] args) {
		double a = Math.log1p(100.90 + 150.10 - 220.50 + 10.50);
		System.out.println("Log1p Result = " + a);	

		System.out.println("\nLog1p Result of Zero: " + Math.log1p(0));
		
		System.out.println("\nLog1p Result of Positive Number: " + Math.log1p(1));
		System.out.println("Log1p Result of Positive Number: " + Math.log1p(10.95));
		
		System.out.println("\nLog1p Result of Negative Number: " + Math.log1p(-2.70));
		System.out.println("Log1p Result of Negative Number: " + Math.log1p(-6.50));		
	}
}
Java log1p Function 1

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

double a = Math.log1p(100.90 + 150.10 - 220.50 + 10.50);
System.out.println("Log1p Result = " + a);

Next, We used the Java log1p Function directly on Positive double values.

System.out.println("\nLog1p Result of Positive Number: " + Math.log1p(1));
System.out.println("Log1p Result of Positive Number: " + Math.log1p(10.95));

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

System.out.println("\nLog1p Result of Negative Number: " + Math.log1p(-2.70));
System.out.println("Log1p Result of Negative Number: " + Math.log1p(-6.50));

Java log1p on Array example

In this Java program, we show how to find the natural logarithm of bulk data. Here, we are going to declare an array of double type and find the natural logarithm of each element in an array.

package MathFunctions;

public class Log1pMethodOnArrays {
	public static void main(String[] args) {
		
		double [] logArray = {21.35, 44.98, -58.41, -9.5987, -0, 92.38, 14.4897};

		for (int i = 0; i < logArray.length; i++) {
			System.out.format("Log1p Result of Array Element = %.4f\n", Math.log1p(logArray[i]));
		}
	}
}
Log1p Result of Array Element = 3.1068
Log1p Result of Array Element = 3.8282
Log1p Result of Array Element = NaN
Log1p Result of Array Element = NaN
Log1p Result of Array Element = 0.0000
Log1p Result of Array Element = 4.5367
Log1p Result of Array Element = 2.7402

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 For 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++) {

If you observe the code snippet, we used the log1p Math function directly inside the System.out.format statement. Here, the Jcompiler will call the Java Math.log1p method ( static double log1p(double number) ) to find the natural logarithm of corresponding values.

System.out.format("Log1p Result of Array Element = %.4f\n", Math.log1p(logArray[i]));

NOTE: If you want to find the natural logarithm of a single item then use: Math.log1p(myArray[index_position])

Java log1p function on Arraylist example

In this Java program, we are going to declare an ArrayList of double type and find the natural logarithm of list elements.

package MathFunctions;

import java.util.ArrayList;

public class Log1pMethodOnArrayList {
	public static void main(String[] args) {
		
		ArrayList<Double> logpList = new ArrayList<Double>(5);
		logpList.add(245.998);
		logpList.add(-480.05);
		logpList.add(250.9876);
		logpList.add(140.01);
		logpList.add(2015.98);
		logpList.add(-897.1898);
		
		for (double x : logpList) {
			System.out.println("Log1p Result of ArrayList =  " + Math.log1p(x));
		}
	}
}
Log1p Result of ArrayList =  5.509380239429203
Log1p Result of ArrayList =  NaN
Log1p Result of ArrayList =  5.529379879951545
Log1p Result of ArrayList =  4.948830809849139
Log1p Result of ArrayList =  7.60935662218864
Log1p Result of ArrayList =  NaN

Within the Java log1p function, first, We declared an ArrayList of double type and assigned some random values.

ArrayList<Double> logpList = new ArrayList<Double>(5);
logpList.add(245.998);
logpList.add(-480.05);
logpList.add(250.9876);
logpList.add(140.01);
logpList.add(2015.98);
logpList.add(-897.1898);

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

for (double x : logpList) {

The following Java statements will print the output. Here, the Jcompiler calls the java Math.log1p method ( static double log1p(double x) ) to find the natural logarithm of corresponding values.

System.out.println("Log1p Result of ArrayList =  " + Math.log1p(x));