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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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".

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more