Home » Java programming language

Java StrictMath log1p() method with example

StrictMath Class log1p() method: Here, we are going to learn about the log1p() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 26, 2019

StrictMath Class log1p() method

  • log1p() method is available in java.lang package.
  • log1p() method is used to return (the logarithm of the sum of the given argument and 1 like log(1+d) in the method.
  • log1p() method is a static method so it is accessible with the class name and if we try to access the method with the class object then also we will not get an error.
  • We need to remember one thing if we pass smaller values for the given argument so the final calculated result of log1p(d) is nearer to the exact result of ln(1+d) than the double floating-point calculation of log(1.0+d).
  • log1p() method does not throw any exception.

Syntax:

    public static double log1p(double d);

Parameter(s):

  • double d – represents the double type argument.

Return value:

The return type of this method is double – it returns the logarithm (1+d) of the given argument.

Note:

  • If we pass NaN, method returns NaN.
  • If we a value which is less than -1, method returns NaN.
  • If we pass a positive infinity, method returns the same (i.e. positive infinity).
  • If we pass a negative infinity, method returns NaN.
  • If we pass 0 (negative or positive), method returns the same with the same sign.

Example:

// Java program to demonstrate the example
// of log1p(double d) method of StrictMath class.

public class Log1p {
    public static void main(String[] args) {
        // variable declarations
        double d1 = 7.0 / 0.0;
        double d2 = -7.0 / 0.0;
        double d3 = 0.0;
        double d4 = -0.0;
        double d5 = 6054.2;

        // Display previous value of d1,d2,d3,d4 and d5
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
        System.out.println("d4: " + d4);
        System.out.println("d5: " + d5);

        // Here , we will get (Infinity) because we are 
        // passing parameter whose value is (Infinity)
        System.out.println("StrictMath.log1p(d1): " + StrictMath.log1p(d1));

        // Here , we will get (NaN) because we are 
        // passing parameter whose value is (-Infinity)
        System.out.println("StrictMath.log1p(d2): " + StrictMath.log1p(d2));

        // Here , we will get (0.0) because we are 
        // passing parameter whose value is (0.0)
        System.out.println("StrictMath.log1p(d3): " + StrictMath.log1p(d3));

        // Here , we will get (-0.0) because we are 
        // passing parameter whose value is (-0.0)
        System.out.println("StrictMath.log1p(d4): " + StrictMath.log1p(d4));

        // Here , we will get (log [1 + d5]) and we are 
        // passing parameter whose value is (6054.2)
        System.out.println("StrictMath.log1p(d5): " + StrictMath.log1p(d5));
    }
}

Output

d1: Infinity
d2: -Infinity
d3: 0.0
d4: -0.0
d5: 6054.2
StrictMath.log1p(d1): Infinity
StrictMath.log1p(d2): NaN
StrictMath.log1p(d3): 0.0
StrictMath.log1p(d4): -0.0
StrictMath.log1p(d5): 8.708672685994957


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.