Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using log base 10 in a formula in Java

Tags:

java

logarithm

I'm trying to write a Java program that can take values and put them into a formula involving log base 10.

How can I calculate log10 in Java?

like image 341
CodyBugstein Avatar asked Dec 04 '12 00:12

CodyBugstein


People also ask

What does log () do in Java?

log() method returns the natural logarithm (base e) of a double value as a parameter. There are various cases : If the argument is NaN or less than zero, then the result is NaN. If the argument is positive infinity, then the result is positive infinity.


2 Answers

It looks like Java actually has a log10 function:

Math.log10(x) 

Otherwise, just use math:

Math.log(x) / Math.log(10) 

http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

like image 92
Mysticial Avatar answered Oct 03 '22 09:10

Mysticial


Math.log10(x) 

suffices for floating-point numbers.

If you need integer log base 10, and you can use third-party libraries, Guava provides IntMath.log10(int, RoundingMode). (Disclosure: I contribute to Guava.)

like image 25
Louis Wasserman Avatar answered Oct 03 '22 10:10

Louis Wasserman