log() in Java
Overview
The log() method in Java Math class returns natural logarithm of a value.
Natural logarithm means the base is fixed as "" and the method returns the value of logarithm of the argument to the base "". Natural or means it has a base of "", where ...
Mathematically logarithm of a number is given by the equation:
Java Math log() method
The log() method of Math class is used to find the natural logarithm of the given argument i.e. log of the argument to the base e where e is the exponential constant.
Syntax of Math log() in Java
This is a static method so we don't need an instance of Math class to invoke this method, rather we can use it directly. By default, the base is fixed to "e".
Parameters of Math log() in Java
It takes a single argument:
- number: A double value whose log needs to be find out.
If the input is not of double type, the compiler tries to convert the input to double format if possible.
Return Value of Math log() in Java
Java Math log() method returns the value of log of the argument to the base . However, there are some special cases:
Value of "a" | Return Value |
---|---|
NaN | NaN |
Negative number | NaN |
Positive Zero or Negative Zero | Negative Infinity |
Positive Infinity | Positive Infinity |
Exception of Math log() in Java
No exception is thrown by log() method of Math class in Java.
Some special cases of errors:
- If we pass a String as input to the log() function it raises an error of incompatible types as String cannot be converted to double.
- However, if we pass a character to the log() method, it does not throw an error as the value of the character is converted to double.
- Example: Maths.log('e') is same as Maths.log(101) and returns a value of but Maths.log("e") throws an error.
Example of Math log() in Java
Output:
Math log() in Java
We'll check the table of return values given above.
Example 1: Radioactive Material Age
We'll take a real-life example. The age of radioactive material is given by , where is a constant for a particular element and A is the present amount while Ao is the initial amount.
Output:
The above code computes the age of the sample to be nearly years.
Example 2: Negative Input
If the input/argument is negative, the output is because logarithm of negative numbers is not defined.
Output:
Example 3: Positive Infinity
If the input is positive infinity, the output is positive infinity because logarithm to any base at positive infinity gives positive infinity.
Output:
Example 4: Zero Input
If the input is zero, the output is negative infinity because logarithm to any base at 0 gives negative infinity.
Output:
Example 5: Custom log() method
Custom log() method of Java returns a natural logarithm i.e. the base is "". But what if I've to find a logarithm of to the base rather than . For that, we'll use the change-of-base formula given below.
To get the logarithm of an inputted value to a custom base, we'll divide the natural logarithm of the inputted value by the natural logarithm of custom base.
Output:
Explanation:
- We pass a number and a custom base as input to our custom (number,custom_base) method.
- calculates natural logarithms of the number and the custom base.
- Using the change of base formula the method divides the natural logarithms computed in the previous step to get the solution.
Conclusion
- The log() method of Math class in Java returns the natural logarithm of a value, i.e. the base of the logarithm is "".
- Result is NaN if the input is NaN or negative.
- Result is positive infinity if the input is positive infinity.
- Result is negative infinity if the input is positive zero or negative zero.
- It does NOT throw any exception(s). However, if the input is a String (and not character), it throws an error because a String instance cannot be converted into a double value.