Log functions in Java


The log functions in Java are part of java.lang.Math. The functions include log, log10, log1p. Let us see an example of each of these log functions −

static double log(double a)

The java.lang.Math.log(double a) returns the natural logarithm (base e) of a double value. Let us see an example −

Example

import java.io.*;
public class Main {
   public static void main(String args[]) {
      // get two double numbers
      double x = 60984.1;
      double y = -497.99;
      // get the natural logarithm for x
      System.out.println("Math.log(" + x + ")=" + Math.log(x));
      // get the natural logarithm for y
      System.out.println("Math.log(" + y + ")=" + Math.log(y));
   }
}

Output

Math.log(60984.1)=11.018368453441132
Math.log(-497.99)=NaN

static double log10(double a)

The java.lang.Math.log10(double a) returns the base 10 logarithm of a double value. Let us now see an example −

Example

import java.io.*;
public class Main {
   public static void main(String args[]) {
      // get two double numbers
      double x = 60984.1;
      double y = 1000;
      // get the base 10 logarithm for x
      System.out.println("Math.log10(" + x + ")=" + Math.log10(x));
      // get the base 10 logarithm for y
      System.out.println("Math.log10(" + y + ")=" + Math.log10(y));
   }
}

Output

Math.log10(60984.1)=4.78521661890635
Math.log10(1000.0)=3.0

static double log1p(double x)

The java.lang.Math.log1p(double x) returns the natural logarithm of the sum of the argument and 1.

Example

import java.io.*;
public class Main {
   public static void main(String args[]) {
      // get two double numbers
      double x = 60984.1;
      double y = 1000;
      // call log1p and print the result
      System.out.println("Math.log1p(" + x + ")=" + Math.log1p(x));
      // call log1p and print the result
      System.out.println("Math.log1p(" + y + ")=" + Math.log1p(y));
   }
}

Output

Math.log1p(60984.1)=11.018384851023473
Math.log1p(1000.0)=6.90875477931522

Updated on: 23-Sep-2019

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements