在 Java 中获取值的以 10 为底的对数
要获取 Java 中 value 的以 10 为底的对数,我们使用 java.lang.Math.log10() 方法。该方法以 double 的形式返回 value 的以 10 为底的对数。如果一个数是 10n,则结果为 n。如果传递的值为 NaN 或负值,则结果为 NaN。如果传递的值为正无穷大,则返回的值为正无穷大。当参数为正零或负零时,则返回负无穷大。
声明 −java.lang.Math.log10() 方法的声明如下 −
public static double log10(double a)
其中 a 为要找出其以 10 为底的对数值。
让我们看一个程序,该程序使用 Math.log10() 方法获取 Java 中某个数字的以 10 为底的对数。
示例
import java.lang.Math;
public class Example {
public static void main(String[] args) {
// declaring and initializing two double values
double x = 300.0;
double y = 100000;
// printing their base 10 logarithms
System.out.println("Base 10 logarithm of "+ x + " is " + Math.log10(x));
System.out.println("Base 10 logarithm of "+ y + " is " + Math.log10(y));
}
}输出
Base 10 logarithm of 300.0 is 2.4771212547196626 Base 10 logarithm of 100000.0 is 5.0
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP