GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Math.log10() Method in Java
  • Math.exp() Method in Java
  • Math.pow() Method in Java
  • Java Math Class

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java Math log() method with example - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java Math log() method with example

Last Updated : 21 Jan, 2026

The java.lang.Math.log() method returns the natural logarithm (base e) of a double value as a parameter. There are various 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 a)

  • Parameter: "a" double value whose natural logarithm is to be calculated.
  • Return Type: Returns a double value representing ln(a) (natural logarithm of a).

Example 1: This program demonstrates how the Math.log() method behaves for different types of input values in Java.

java
class GFG {
    public static void main(String[] args) {
        double a = -2.55;
        double b = 1.0 / 0;
        double c = 0;
        double d = 145.256;
        System.out.println(Math.log(a));
        System.out.println(Math.log(b));
        System.out.println(Math.log(c));
        System.out.println(Math.log(d));
    }
}

Output
NaN
Infinity
-Infinity
4.978497702968366

Explanation:

  • Math.log(a) returns NaN because the logarithm of a negative number is undefined.
  • Math.log(b) returns Infinity since the input value is positive infinity.
  • Math.log(c) returns -Infinity because the logarithm of zero tends toward negative infinity.
  • Math.log(d) returns the natural logarithm (base e) of a positive number.

Example 2: This program shows how to compute the natural logarithm (base e) of a number using the Math.log() method in Java.

Java
import java.io.*;
class GFG {
    public static void main(String[] args)
    {
        double number = 10.0;
        double result = Math.log(number);
        System.out.println(result);
    }
}

Output
2.302585092994046

Explanation:

  • number stores the input value whose logarithm is to be calculated.
  • Math.log(number) computes the natural logarithm of 10.0.
  • The result is stored in result and printed to the console.

Related Topics

  • Math.log10() Method in Java
  • Math.exp() Method in Java
  • Math.pow() Method in Java
  • Java Math Class
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.