Wednesday, July 3, 2024
HomeLanguagesJavaStrictMath log() Method In Java

StrictMath log() Method In Java

The Java.lang.StrictMath.log() is an inbuilt method of StrictMath class which is used to calculate the natural logarithm i.e., log with base e, of a given double value. It gives rise to three special results: 

  • It returns a positive infinity when the argument is positive infinity.
  • It returns NaN when the argument is NaN or less than zero.
  • The result is negative infinity when the argument is positive zero or negative zero.

Syntax:  

public static double log(double num)

Parameters: The method accepts one parameter num of double type whose logarithmic value is to be found.
Return Value: The method returns the value of natural logarithm of num.
Examples :  

Input: num = 5.0 
Output: 1.6094379124341003

Input: num = 10.0 
Output:  2.302585092994046

Below programs illustrate the Java.lang.StrictMath.log() Method: 
Program 1:  

java




// Java program to illustrate the
// Java.lang.StrictMath.log() Method
import java.lang.*;
 
public class Geeks {
 
public static void main(String[] args) {
 
    double num1 = 10 , num2 = 25.2 ;
     
    // It returns natural logarithm(base e)
    double log_Value = StrictMath.log(num1);
    System.out.print("Log value of " + num1 + " = " );
    System.out.println(log_Value);
 
    log_Value = StrictMath.log(num2);
    System.out.print("Log value of " + num2 + " = " );
    System.out.println(log_Value);
 
}
}


Output: 

Log value of 10.0 = 2.302585092994046
Log value of 25.2 = 3.2268439945173775

 

Program 2: 

java




// Java program to illustrate the
// Java.lang.StrictMath.log() Method
import java.lang.*;
 
public class Geeks {
 
public static void main(String[] args) {
 
    double num1 = 0 , num2 = (1.0/0.0) , num3 = 1;
     
    // It returns natural logarithm(base e)
    double log_Value = StrictMath.log(num1);
    System.out.print("Log value of " + num1 + " = " );
    System.out.println(log_Value);
     
    log_Value = StrictMath.log(num2);
    System.out.print("Log value of " + num2 + " = " );
    System.out.println(log_Value);
     
    log_Value = StrictMath.log(num3);
    System.out.print("Log value of " + num3 + " = " );
    System.out.println(log_Value);
 
}
}


Output: 

Log value of 0.0 = -Infinity
Log value of Infinity = Infinity
Log value of 1.0 = 0.0

 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments