log() in Java

Learn via video courses
Topics Covered

Overview

The log() method in Java Math class returns natural logarithm of a value.

Natural logarithm means the base is fixed as "ee" and the method returns the value of logarithm of the argument to the base "ee". Natural loglog or lnln means it has a base of "ee", where e=2.718e=2.718...

Mathematically logarithm of a number is given by the equation:

logarithm of a number

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 ee. However, there are some special cases:

Value of "a"Return Value
NaNNaN
Negative numberNaN
Positive Zero or Negative ZeroNegative Infinity
Positive InfinityPositive 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 ASCIIASCII value of the character is converted to double.
  • Example: Maths.log('e') is same as Maths.log(101) and returns a value of 4.614.61 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 T=(1/λ)ln(A/Ao)T = -(1/λ)*ln(A/Ao), 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 2020 years.

Example 2: Negative Input

If the input/argument is negative, the output is NaNNaN 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 "ee". But what if I've to find a logarithm of 2525 to the base 55 rather than ee. For that, we'll use the change-of-base formula given below. Custom Log

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 logBlogB(number,custom_base) method.
  • logB()logB() 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 "ee".
  • 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.