×

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x).

Syntax:

public static double log1p (double a)

Parameters:

The parameter ‘a’ represents a double value.

Return Value:

The log1p() method returns the natural log of x +1.

Special cases of the log1p() method are as follows:

  • It returns NaN, if the argument is NaN or less than -1.
  • It returns positive infinity, if the argument passed is positive infinite.
  • It returns negative infinity, if the argument passed is negative one.
  • It returns zero with the same sign as the argument, if the argument passed is equal to zero.

Example 1:

public class JavaMathLog1pExample1 {
    public static void main(String[] args) {
        double a = 10;
        // returns the natural log of x +1
        System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a));
    }
}

Output:

Logrithm 1p value for 10.0 : 2.3978952727983707

Example 2:

public class JavaMathLog1pExample2 {
    public static void main(String[] args) {
        double a = -100;
        //returns NaN, if the argument is NaN or less than -1
        System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a));
    }
}

Output:

Logrithm 1p value for -100.0 : NaN

Example 3:

public class JavaMathLog1pExample3 {
    public static void main(String[] args) {
        double a = 10/0.0d;
        // positive infinity, if the argument passed is positive infinite
        System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a));
    }
}

Output:

Logrithm 1p value for Infinity : Infinity

Example 4:

public class JavaMathLog1pExample4 {
    public static void main(String[] args) {
        double a = -1;
        // returns negative infinity, if the argument passed is negative one
        System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a));
    }
}

Output:

Logrithm 1p value for -1.0 : -Infinity

Example 5:

public class JavaMathLog1pExample5 {
    public static void main(String[] args) {
        double a = -0;
        //returns zero with the same sign as the argument, if the argument passed is equal to zero
        System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a));
    }
}

Output:

Logrithm 1p value for 0.0 : 0.0

Example 6:

public class JavaMathLog1pExample6 {
    public static void main(String[] args) {
        double a = -100;
        // returns NaN, if the argument passed is NaN or less than -1
        System.out.println("Logrithm 1p value for "+a +" : "+Math.log1p(a));
    }
}

Output:

Logrithm 1p value for -100.0 : NaN

Related Topics

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

Web Service Response Time Calculation in Java

Administration response time or reaction time is the typical measure of time it takes for a solicitation to be handled by a PC framework, like your organization switch. In the...

4 minutes read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action. Threads are autonomous. If an exception occurs in one thread,...

6 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Java String toUpperCase() methods

Java String toUpperCase() method is used to convert all the characters of the String into upper case. Syntax public String toUpperCase() public String toUpperCase(Locale locale) Returns It returns upper case String. Java String toUpperCase() Example 1: public...

1 minute read.

How to use Lambda Expression in Java?

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single method interface using an...

4 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Set Value to Enum in Java

In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to...

3 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.