- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Math log() Method
Description
The Java Math log(double a) returns the natural logarithm (base e) of a double value. Special cases:
If the argument is NaN or less than zero, then the result is NaN.
If the argument is positive infinity, then the result is positive infinity.
If the argument is positive zero or negative zero, then the result is negative infinity.
Syntax
public static double log(double d)
Parameters
Here is the detail of parameters −
d − Any primitive data type.
Return Value
- This method returns the natural logarithm of the argument.
Example 1
In this example, we're showing the usage of Math.log() method to get the log of a double number. We've created two double variables x, y and initialized them with different values. Then using Math.log() method we're printing the log of a double value.
public class Test { public static void main(String args[]) { double x = 11.635; double y = 2.76; System.out.printf("The value of e is %.4f%n", Math.E); System.out.printf("log(%.3f) is %.3f%n", x, Math.log(x)); } }
This will produce the following result −
Output
The value of e is 2.7183 log(11.635) is 2.454
Example 2
In this example, we're showing the usage of Math.log() method to get the log of a float number. We've created two float variables x, y and initialized them with different values. Then using Math.log() method we're printing the log of a float value.
public class Test { public static void main(String args[]) { float x = (float)11.635; float y = (float)2.76; System.out.printf("The value of e is %.4f%n", Math.E); System.out.printf("log(%.3f) is %.3f%n", x, Math.log(x)); } }
This will produce the following result −
Output
The value of e is 2.7183 log(11.635) is 2.454
Example 3
In this example, we're showing the usage of Math.log() method to get the log of zero. We've created a double variable and initialized it with 0.0. Then using Math.log() method we're printing the log of a zero value.
public class Test { public static void main(String args[]) { double x = 0.0; System.out.printf("The value of e is %.4f%n", Math.E); System.out.printf("log(%.3f) is %.3f%n", x, Math.log(x)); } }
This will produce the following result −
Output
The value of e is 2.7183 log(0.000) is -Infinity
To Continue Learning Please Login
Login with Google