How to Calculate Logarithm in Java

Sheeraz Gul Feb 02, 2024
How to Calculate Logarithm in Java

This tutorial will demonstrate how to calculate logarithm in Java using the Math.log function.

Use Math.log to Calculate the Logarithm of the Given Value in Java

In Java, the java.lang.Math library has the function Math.log() to calculate the logarithm of a given value. The input value can be a double, integer, or float and returns a double value.

We must ensure that the number is not negative, zero, or infinity; otherwise, the output will not be a double data type.

The example below shows the use of Math.log in Java.

import java.lang.Math;

class Java_Log {
  public static void main(String args[]) {
    double p = -4.3;
    double q = 6.0 / 0;
    double r = 0;
    double s = 130.333;
    double u = 130.333 / 30;
    int v = 5;
    float w = 34;

    // The negative double in the Math.log function will output: NaN
    System.out.println("The Output for Negative Integer:");
    System.out.println(Math.log(p));

    // The positive infinity in the Math.log function will output: Infinity
    System.out.println("The Output for Positive Infinity:");
    System.out.println(Math.log(q));

    // The positive zero in the Math.log function will output: - Infinity
    System.out.println("The Output for Zero:");
    System.out.println(Math.log(r));

    // The positive double argument in the Math.log function will output: logarithm answer
    System.out.println("The Output for positive double:");
    System.out.println(Math.log(s));

    // The positive double argument in the Math.log function will output: logarithm answer
    System.out.println("The Output for Positive double in division form:");
    System.out.println(Math.log(u));

    // The positive integer argument in the Math.log function will output: logarithm answer
    System.out.println("The Output for Positive Integer:");
    System.out.println(Math.log(v));

    // The positive integer float argument in the Math.log function will output: logarithm answer
    System.out.println("The Output for Positive float:");
    System.out.println(Math.log(w));
  }
}

Output:

The Output for Negative Integer:
NaN
The Output for Positive Infinity:
Infinity
The Output for Zero:
-Infinity
The Output for positive double:
4.870092713769228
The Output for Positive double in division form:
1.468895332107073
The Output for Positive Integer:
1.6094379124341003
The Output for Positive float:
3.5263605246161616

The code above calculates the logarithm for each data type and returns the output values of double, integer, float, infinity, and zero input values.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Math