Math log10() method in Java
public static double log10(double x)
A logarithm is a mathematical operation that is 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 calculate logarithmic effectively. One such logarithmic method is Java Math.log10() . Math log10 is an in-build static method that is present in Java.lang.Math class. Java Math.log10() returns the base 10 logarithm of the argument.
- If the parameter is positive value this method will return the base 10 logarithm value of the parameter.
- 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.log10 With Positive Number
If the parameter is positive number Java Math.log10 method return the base 10 logarithm value.
public class RebootMemory { public static void main(String []args) { System.out.println("Math.log10(50) = "+ Math.log10(50)); System.out.println("Math.log10(50L) = "+ Math.log10(50L)); System.out.println("Math.log10(50f) = "+ Math.log10(50f)); System.out.println("Math.log10(50.0) = "+ Math.log10(50.0)); } }
Math.log10(50) = 1.6989700043360187 Math.log10(50L) = 1.6989700043360187 Math.log10(50f) = 1.6989700043360187 Math.log10(50.0) = 1.6989700043360187
Math.log10 With Negative Number
If the parameter is negative number Java Math.log10 method return NaN (Not a Number)
public class RebootMemory { public static void main(String []args) { System.out.println("Math.log10(-50) = "+ Math.log10(-50)); System.out.println("Math.log10(-50L) = "+ Math.log10(-50L)); System.out.println("Math.log10(-50f) = "+ Math.log10(-50f)); System.out.println("Math.log10(-50.0) = "+ Math.log10(-50.0)); } }
Math.log10(-50) = NaN Math.log10(-50L) = NaN Math.log10(-50f) = NaN Math.log10(-50.0) = NaN
Java Math.log10 With Positive Infinity
Math.log10 method is used to get the base 10 logarithm value of given parameter. but If the parameter is positive Infinity this method will return positive Infinity.
public class RebootMemory { public static void main(String []args) { System.out.println("Math.log10(50/0.0) = "+ Math.log10(50/0.0) ); System.out.println("Math.log10(50L/0.0) = "+ Math.log10(50L/0.0) ); System.out.println("Math.log10(50f/0.0) = "+ Math.log10(50/0.0) ); System.out.println("Math.log10(50.1/0.0) = "+ Math.log10(50.1/0.0) ); System.out.println("Math.log10(50f/0) = "+ Math.log10(50f/0) ); System.out.println("Math.log10(50f/-0) = "+ Math.log10(50f/-0) ); System.out.println("Math.log10(-50/-0f) = "+ Math.log10(-50/-0f) ); System.out.println("Math.log10(50.1/0) = "+ Math.log10(50.1/0) ); System.out.println("Math.log10(50.1/-0) = "+ Math.log10(50.1/-0) ); System.out.println("Math.log10(-50.1/-0.0) = "+ Math.log10(-50/-0.0) ); } }
Math.log10(50/0.0) = Infinity Math.log10(50L/0.0) = Infinity Math.log10(50f/0.0) = Infinity Math.log10(50.1/0.0) = Infinity Math.log10(50f/0) = Infinity Math.log10(50f/-0) = Infinity Math.log10(-50/-0f) = Infinity Math.log10(50.1/0) = Infinity Math.log10(50.1/-0) = Infinity Math.log10(-50.1/-0.0) = Infinity
Java Math.log10 With Negative Infinity
Math.log10 method is used to get the base 10 logarithm value of given parameter. but If the parameter is negative Infinity this method will return NaN (Not a Number).
public class RebootMemory { public static void main(String []args) { System.out.println("Math.log10(50/-0.0) = "+ Math.log10(50/-0.0) ); System.out.println("Math.log10(50L/-0.0) = "+ Math.log10(50L/-0.0) ); System.out.println("Math.log10(50f/-0.0) = "+ Math.log10(50f/-0.0) ); System.out.println("Math.log10(-50f/0.0) = "+ Math.log10(-50f/0.0) ); System.out.println("Math.log10(-50f/0) = "+ Math.log10(-50f/0) ); System.out.println("Math.log10(50.1/-0.0) = "+ Math.log10(50.1/-0.0) ); System.out.println("Math.log10(-50.1/0.0) = "+ Math.log10(-50.1/0.0) ); System.out.println("Math.log10(-50.1/0) = "+ Math.log10(-50.1/0) ); } }
Math.log10(50/-0.0) = NaN Math.log10(50L/-0.0) = NaN Math.log10(50f/-0.0) = NaN Math.log10(-50f/0.0) = NaN Math.log10(-50f/0) = NaN Math.log10(50.1/-0.0) = NaN Math.log10(-50.1/0.0) = NaN Math.log10(-50.1/0) = NaN
Java Math.log10 With NaN
Math.log10 method is used to get the base 10 logarithm value of given parameter. but If the parameter is NaN this method will return NaN (Not a Number).
public class RebootMemory { public static void main(String []args) { System.out.println("Math.log10(0/0.0) = "+ Math.log10(0/0.0) ); System.out.println("Math.log10(0/-0.0) = "+ Math.log10(0/-0.0) ); System.out.println("Math.log10(-0/0.0) = "+ Math.log10(-0/0.0) ); System.out.println("Math.log10(-0/-0.0) = "+ Math.log10(-0/-0.0) ); System.out.println("Math.log10(0/0f) = "+ Math.log10(0/0f) ); System.out.println("Math.log10(0/-0f) = "+ Math.log10(0/-0f) ); System.out.println("Math.log10(-0/0f) = "+ Math.log10(-0/0f) ); System.out.println("Math.log10(-0/-0f) = "+ Math.log10(-0/-0f) ); } }
Math.log10(0/0.0) = NaN Math.log10(0/-0.0) = NaN Math.log10(-0/0.0) = NaN Math.log10(-0/-0.0) = NaN Math.log10(0/0f) = NaN Math.log10(0/-0f) = NaN Math.log10(-0/0f) = NaN Math.log10(-0/-0f) = NaN
Math.log10 with positive or negative Zero
Math.log10 method is used to calculate the base 10 logarithm value of given parameter. but If the parameter is Positive or negative zero this method will return negative Infinity.
public class RebootMemory { public static void main(String []args) { System.out.println("Math.log10(0) = "+ Math.log10(0) ); System.out.println("Math.log10(-0) = "+ Math.log10(-0) ); System.out.println("Math.log10(0L) = "+ Math.log10(0L) ); System.out.println("Math.log10(-0L) = "+ Math.log10(-0L) ); System.out.println("Math.log10(0f) = "+ Math.log10(0f) ); System.out.println("Math.log10(-0f) = "+ Math.log10(-0f) ); System.out.println("Math.log10(0.0) = "+ Math.log10(0.0) ); System.out.println("Math.log10(-0.0) = "+ Math.log10(-0.0) ); } }
Math.log10(0) = -Infinity Math.log10(-0) = -Infinity Math.log10(0L) = -Infinity Math.log10(-0L) = -Infinity Math.log10(0f) = -Infinity Math.log10(-0f) = -Infinity Math.log10(0.0) = -Infinity Math.log10(-0.0) = -Infinity
Calculating Custom log using Math.log10 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). Or Math.log10 (base 10 logarithm).
public class RebootMemory { public static double customLog(double b, double a) { return Math.log10(a) / Math.log10(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.861353116146786 Log 125 base 5 = 2.9999999999999996 Log 75 base 3 = 3.9299470414358546