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

问题描述 投票:30回答:5

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

如何在Java中计算log10?

java logarithm
5个回答
75
投票

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

Math.log10(x)

否则,只需使用数学:

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

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


4
投票
Math.log10(x)

足以满足浮点数。

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


1
投票

你也可以这样做

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

1
投票

以下完整示例可以帮助您

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.