BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.log() Method

By Chaitanya Singh | Filed Under: java

Java Math.log(double num) method returns natural logarithm of the double value num. The natural logarithm is also known as base e logarithm. The e is a constant, whose approximate value is 2.71828.

When base and the value both are equal then log returns 1. Here, we are trying to find out the base e log of value e, result is approximately 1.

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = 2.71828;
    // Base e logarithm of value 2.71828
    System.out.println(Math.log(num));
  }
}

Output:

0.999999327347282

Syntax of Math.log() method

Math.log(10); //returns 2.302585092994046

log() Description

public static double log(double num): Returns the base e logarithm of argument num. The returns type of log() method is double.

log() Parameters

  • num: The double value whose natural logarithm is to be determined.

log() Return Value

  • Returns natural logarithm of double value num.
  • If the value of num is NaN (Not a number) or less than zero then it returns NaN.
  • If num is positive infinity then it returns positive infinity.
  • If argument num is either positive zero or negative zero then it returns negative infinity.

Example 1: Natural logarithm of numbers 2 and 10

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = 2, num2 = 10;
    System.out.println("Log of 2: "+Math.log(num));
    System.out.println("Log of 10: "+Math.log(num2));
  }
}

Output:

Java Math.log() Example Output_1

Example 2: Natural logarithm of negative value or NaN

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = -2, num2 = 0.0/0;
    System.out.println("Log of -ve value: "+Math.log(num));
    System.out.println("Log of NaN: "+Math.log(num2));
  }
}

Output:

Java Math.log() Example Output_2

Example 3: Base e logarithm of zero and infinity

public class JavaExample
{
  public static void main(String[] args)
  {
    double num = 0, num2 = 10.0/0;
    System.out.println("Log of zero: "+Math.log(num));
    System.out.println("Log of infinity: "+Math.log(num2));
  }
}

Output:

Java Math.log() Example Output_3

Recommended Posts

  • Java Math.toIntExact()
  • Java Math.negateExact()
  • Java Math.decrementExact()
  • Java Math.incrementExact()
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap