Java Math log() Method
Java Math Class
Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), abs() etc.
The java.lang.Math class contains various methods for performing basic numeric operations
Here, in the page we will discuss about the math log() Method in java.
Java Math log() Method :
This function returns the double value’s natural logarithm (in base e).
Special Points :
- The result is NaN if the argument is NaN or less than zero.
- Positive infinity is the result if the argument is positive infinity.
- Negative infinity is the outcome if the argument is either positive zero or negative zero.
Syntax :
public static double log(double a)
Parameters :
a - a value.
Throws Exception :
It does throws any exceptions and error.
Return Value :
The natural logarithm of a, or the value of ln a.
Example 1 :
public class Main{ public static void main(String[] args) { //declaring variables double x = 100; double y = 505.5; double z = 90.7; double e = 0.0; //printing output for log method. System.out.println("Math.log(" + x + ") = " + Math.log(x)); System.out.println("Math.log(" + y + ") = " + Math.log(y)); System.out.println("Math.log(" + z + ") = " + Math.log(z)); System.out.println("Math.log(" + e + ") = " + Math.log(e)); } }
Output :
Math.log(100.0) = 4.605170185988092 Math.log(505.5) = 6.225548038460526 Math.log(90.7) = 4.507557357121091 Math.log(0.0) = -Infinity
Explanation :
This example shows how the log method functions using the Java math log() method. It simply returns the logarithmic value .
Example 2 :
public class Main {
public static void main (String args[]) { //declaring variables double a = -4.45; double b = 1.0 / 0; double c = 0; // for negative integer as argument, we get asoutput NAN System.out.println (Math.log (a)); //for positive infinity as argument, we get output Infinity System.out.println (Math.log (b)); // positive zero as argument,we get output -Infinity System.out.println (Math.log (c)); } }
Output :
NaN Infinity -Infinity
Explanation :
In the preceding example, we have observed that :
The result is NaN if the argument is NaN or less than zero. Positive infinity is the result if the argument is positive infinity. Negative infinity is the outcome if the argument is either positive zero or negative zero.
The result is NaN if the argument is NaN or less than zero. Positive infinity is the result if the argument is positive infinity. Negative infinity is the outcome if the argument is either positive zero or negative zero.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment