- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get natural logarithm value using Math.log in Java
To obtain the natural log of a number, we use the java.lang.Math.log() method. The Math.log() method returns the natural logarithm i.e. log to the base e of a double value. If the value passed is NaN or negative, the result is NaN. If the value passed is positive infinity, then the value returned is positive infinity. When the parameter is positive zero or negative zero, then negative infinity is returned.
Declaration - The java.lang.Math.log() method is declared as follows
public static double log(double a)
where a is a value whose natural logarithm is to be found.
Let us see a program to get the natural logarithm of a number in Java using the Math.log() method
Example
import java.lang.Math; public class Example { public static void main(String[] args) { // declaring and initializing two double values double x = 300.01d; double y = 290.344d; // printing their natural logarithms System.out.println("Natural logarithm of "+ x + " is " + Math.log(x)); System.out.println("Natural logarithm of "+ y + " is " + Math.log(y)); } }
Output
Natural logarithm of 300.01 is 5.703815807433991 Natural logarithm of 290.344 is 5.671066426889541
Advertisements
To Continue Learning Please Login
Login with Google