Math log() method in Java
public static double log(double x)
A logarithm is a mathematical function that used to find out how many times a certain number (called the base) needs to be multiplied by itself to produce another number. In simple words To what power must the base be raised to produce the given number?
Java offers Logarithmic methods in its standard library to handle logarithmic operations effectively. One such logarithmic method is Java Math.log(). It is in-build static method that is present in Java.lang.Math class. Java Math.log() is used to get the natural logarithm (base e) value of number.
- If the parameter is positive value this method will return the logarithm of a given value.
- If the parameter is negative value this method will return NaN.
- If the parameter is positive Infinity this method will return positive Infinity
- If the parameter is negative Infinity this method will return NaN.
- If the parameter is NaN this method will return NaN.
- If the parameter is positive or negative Zero this method will return Negative Infinity.
Math.log With Positive Number
If the parameter is positive number Java Math.log method return the natural logarithm (base e) value.
public class Program { public static void main(String []args) { System.out.println("Math.log(10) = "+ Math.log(10)); System.out.println("Math.log(10L) = "+ Math.log(10L)); System.out.println("Math.log(10f) = "+ Math.log(10f)); System.out.println("Math.log(10.0) = "+ Math.log(10.0)); } }
Math.log(10) = 2.302585092994046 Math.log(10L) = 2.302585092994046 Math.log(10f) = 2.302585092994046 Math.log(10.0) = 2.302585092994046
Math.log With Negative Number
If the parameter is negative number Java Math.log method return NaN (Not a Number)
public class Program { public static void main(String []args) { System.out.println("Math.log(-10) = "+ Math.log(-10)); System.out.println("Math.log(-10L) = "+ Math.log(-10L)); System.out.println("Math.log(-10f) = "+ Math.log(-10f)); System.out.println("Math.log(-10.0) = "+ Math.log(-10.0)); } }
Math.log(-10) = NaN Math.log(-10L) = NaN Math.log(-10f) = NaN Math.log(-10.0) = NaN
Java Math.log With Positive Infinity
Math.log method is used to get the natural logarithm (base e) value of given parameter. but If the parameter is positive Infinity this method will return positive Infinity.
public class Program { public static void main(String []args) { System.out.println("Math.log(10/0.0) = "+ Math.log(10/0.0) ); System.out.println("Math.log(10L/0.0) = "+ Math.log(10L/0.0) ); System.out.println("Math.log(10f/0.0) = "+ Math.log(10/0.0) ); System.out.println("Math.log(10.1/0.0) = "+ Math.log(10.1/0.0) ); System.out.println("Math.log(10f/0) = "+ Math.log(10f/0) ); System.out.println("Math.log(10f/-0) = "+ Math.log(10f/-0) ); System.out.println("Math.log(-10/-0f) = "+ Math.log(-10/-0f) ); System.out.println("Math.log(10.1/0) = "+ Math.log(10.1/0) ); System.out.println("Math.log(10.1/-0) = "+ Math.log(10.1/-0) ); System.out.println("Math.log(-10.1/-0.0) = "+ Math.log(-10/-0.0) ); } }
Math.log(10/0.0) = Infinity Math.log(10L/0.0) = Infinity Math.log(10f/0.0) = Infinity Math.log(10.1/0.0) = Infinity Math.log(10f/0) = Infinity Math.log(10f/-0) = Infinity Math.log(-10/-0f) = Infinity Math.log(10.1/0) = Infinity Math.log(10.1/-0) = Infinity Math.log(-10.1/-0.0) = Infinity
Java Math.log With Negative Infinity
Math.log method is used to get the natural logarithm (base e) value of given parameter. but If the parameter is negative Infinity this method will return NaN (Not a Number).
public class Program { public static void main(String []args) { System.out.println("Math.log(10/-0.0) = "+ Math.log(10/-0.0) ); System.out.println("Math.log(10L/-0.0) = "+ Math.log(10L/-0.0) ); System.out.println("Math.log(10f/-0.0) = "+ Math.log(10f/-0.0) ); System.out.println("Math.log(-10f/0.0) = "+ Math.log(-10f/0.0) ); System.out.println("Math.log(-10f/0) = "+ Math.log(-10f/0) ); System.out.println("Math.log(10.1/-0.0) = "+ Math.log(10.1/-0.0) ); System.out.println("Math.log(-10.1/0.0) = "+ Math.log(-10.1/0.0) ); System.out.println("Math.log(-10.1/0) = "+ Math.log(-10.1/0) ); } }
Math.log(10/-0.0) = NaN Math.log(10L/-0.0) = NaN Math.log(10f/-0.0) = NaN Math.log(-10f/0.0) = NaN Math.log(-10f/0) = NaN Math.log(10.1/-0.0) = NaN Math.log(-10.1/0.0) = NaN Math.log(-10.1/0) = NaN
Java Math.log With NaN
Math.log method is used to get the natural logarithm (base e) value of given parameter. but If the parameter is NaN this method will return NaN (Not a Number).
public class Program { public static void main(String []args) { System.out.println("Math.log(0/0.0) = "+ Math.log(0/0.0) ); System.out.println("Math.log(0/-0.0) = "+ Math.log(0/-0.0) ); System.out.println("Math.log(-0/0.0) = "+ Math.log(-0/0.0) ); System.out.println("Math.log(-0/-0.0) = "+ Math.log(-0/-0.0) ); System.out.println("Math.log(0/0f) = "+ Math.log(0/0f) ); System.out.println("Math.log(0/-0f) = "+ Math.log(0/-0f) ); System.out.println("Math.log(-0/0f) = "+ Math.log(-0/0f) ); System.out.println("Math.log(-0/-0f) = "+ Math.log(-0/-0f) ); } }
Math.log(0/0.0) = NaN Math.log(0/-0.0) = NaN Math.log(-0/0.0) = NaN Math.log(-0/-0.0) = NaN Math.log(0/0f) = NaN Math.log(0/-0f) = NaN Math.log(-0/0f) = NaN Math.log(-0/-0f) = NaN
Math.log() with positive or negative Zero
Math.log method is used to get the natural logarithm (base e) value of given parameter. but If the parameter is Positive or negative zero this method will return negative Infinity.
public class Program { public static void main(String []args) { System.out.println("Math.log(0) = "+ Math.log(0) ); System.out.println("Math.log(-0) = "+ Math.log(-0) ); System.out.println("Math.log(0L) = "+ Math.log(0L) ); System.out.println("Math.log(-0L) = "+ Math.log(-0L) ); System.out.println("Math.log(0f) = "+ Math.log(0f) ); System.out.println("Math.log(-0f) = "+ Math.log(-0f) ); System.out.println("Math.log(0.0) = "+ Math.log(0.0) ); System.out.println("Math.log(-0.0) = "+ Math.log(-0.0) ); } }
Math.log(0) = -Infinity Math.log(-0) = -Infinity Math.log(0L) = -Infinity Math.log(-0L) = -Infinity Math.log(0f) = -Infinity Math.log(-0f) = -Infinity Math.log(0.0) = -Infinity Math.log(-0.0) = -Infinity
Calculating Custom log using Math.log method
Java does not provide a method for custom base logarithms in the Math class. However, you can compute custom base log by using Math.log (natural logarithm).
public class Program { public static double customLog(double b, double a) { return Math.log(a) / Math.log(b); } public static void main(String []args) { System.out.println("Log 10 base 10 = "+ customLog(10,10) ); System.out.println("Log 100 base 10 = "+ customLog(10,100) ); System.out.println("Log 100 base 5 = "+ customLog(5,100) ); System.out.println("Log 125 base 5 = "+ customLog(5,125) ); System.out.println("Log 75 base 3 = "+ customLog(3,75) ); } }
Log 10 base 10 = 1.0 Log 100 base 10 = 2.0 Log 100 base 5 = 2.8613531161467867 Log 125 base 5 = 3.0000000000000004 Log 75 base 3 = 3.9299470414358537