Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Java log1p Function

by suresh

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));		
	}
}

OUTPUT

Java log1p Function 1

ANALYSIS

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]));
		}
	}
}

OUTPUT

Java log1p Function 2

ANALYSIS

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));
		}
	}
}

OUTPUT

Java log1p Function 3

ANALYSIS

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));

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy