在Java中的公式中使用日志库10

我正在尝试编写一个可以获取值的Java程序,并将它们放入涉及日志库10的公式中。

如何在Java中计算log 10

看起来Java实际上有一个log10函数:

 Math.log10(x) 

否则,只需使用数学:

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

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

 Math.log10(x) 

足以满足浮点数。

如果您需要整数日志库10,并且可以使用第三方库,则Guava提供IntMath.log10(int, RoundingMode) 。 (披露:我向番石榴捐款。)

你也可以这样做

 class log { public void main(double a) { double l = 0; l = Math.log10(a); System.out.println(l); } } 

以下完整示例可以帮助您

 public class Log10Math { public static void main(String args[]) { // Method returns logarithm of number argument with base ten(10); short shortVal = 10; byte byteVal = 1; double doubleVal = Math.log10(byteVal); System.out.println("Log10 Results"); System.out.println(Math.log10(1)); System.out.println(doubleVal); System.out.println(Math.log10(shortVal)); } } 

产量

 Log10 Results 0.0 0.0 1.0 

在Java中我们可以使用日志function。 (int)Math.log(val); int是一个将其转换为此类型的类型,您可以在其中存储您的答案。