Java log10 是一个 Math 库 函数,它返回给定数字的以 10 为底的对数值。Math.log10 的语法接受正的 double 值并返回以 10 为底的对数值。
static double log10(double number); //Return Type is Double // To use in program: Math.log10(double number);
Java log10 函数示例
在这个 Math 程序中,我们将找到不同数据的 log10 值。
package MathFunctions;
public class Log10Method {
public static void main(String[] args) {
double a = Math.log10(100.90 + 150.10 - 220.50 + 10.50);
System.out.println("Logarithmic Value = " + a);
System.out.println("\nLogarithmic Value of Zero: " + Math.log10(0));
System.out.println("\nLogarithmic Value of Positive Number: " + Math.log10(1));
System.out.println("Logarithmic Value of Positive Number: " + Math.log10(10.95));
System.out.println("\nLogarithmic Value of Negative Number: " + Math.log10(-8.70));
System.out.println("Logarithmic Value of Negative Number: " + Math.log10(-5.00));
}
}

首先,我们声明一个 Double 类型的变量,并直接对表达式执行 Math.log10 函数。
double a = Math.log10(100.90 + 150.10 - 220.50 + 10.50);
System.out.println("Logarithmic Value = " + a);
接下来,我们直接对正的 double 值使用Math 函数。
System.out.println("\nLogarithmic Value of Positive Number: " + Math.log10(1));
System.out.println("Logarithmic Value of Positive Number: " + Math.log10(10.95));
接下来,我们直接对负的 double 值使用 Math.log10 函数。从截图中可以看出,它为负值返回 NaN。
System.out.println("\nLogarithmic Value of Negative Number: " + Math.log10(-8.70));
System.out.println("Logarithmic Value of Negative Number: " + Math.log10(-5.00));
Java log10 数组示例
在这个Java 程序中,我们展示了如何找到批量数据的以 10 为底的对数值。在这里,我们将声明一个 double 类型的数组,并找到数组中每个元素的以 10 为底的对数值。
package MathFunctions;
public class Log10MethodOnArrays {
public static void main(String[] args) {
double [] logArray = {20.35, 54.98, -68.41, -9.5987, -0, 120.38, 14.4897};
for (int i = 0; i < logArray.length; i++) {
System.out.format("Logarithmic Value of Array Element = %.4f\n", Math.log10(logArray[i]));
}
}
}
Logarithmic Value of Array Element = 1.3086
Logarithmic Value of Array Element = 1.7402
Logarithmic Value of Array Element = NaN
Logarithmic Value of Array Element = NaN
Logarithmic Value of Array Element = -Infinity
Logarithmic Value of Array Element = 2.0806
Logarithmic Value of Array Element = 1.1611
在这个 Java log10 函数示例中,首先,我们声明一个 double 类型的数组并为其分配一些随机值。
double [] logArray = {20.35, 54.98, -68.41, -9.5987, -0, 120.38, 14.4897};
接下来,我们使用Java For Loop 遍历数组。在 For Loop 中,我们将 i 值初始化为 0。接下来,编译器将检查条件(i < logArray.length)。只要循环条件为 True,循环内的语句就会执行。
提示:logArray.length 用于查找数组的长度。
for (int i = 0; i < logArray.length; i++) {
以下Java 语句将打印输出。如果您观察代码片段,我们直接在 System.out.format 语句中使用了 log10 函数。在这里,编译器将调用静态 double log10(double number) 方法来查找相应值的以 10 为底的对数值。
System.out.format("Logarithmic Value of Array Element = %.4f\n", Math.log10(logArray[i]));
注意:如果您想找到单个项的以 10 为底的对数值,请使用:Math.log10(myArray[index_position])
Java log10 函数 ArrayList 示例
在这个 程序中,我们将声明一个 double 类型的 ArrayList,并找到列表元素的以 10 为底的对数值。
在此示例中,首先,我们声明一个 double 类型的 ArrayList 并为其分配一些随机值。接下来,我们使用 For Loop 遍历 ArrayList 中的 double 值。
System.out.println 语句将打印输出。在这里,Jcompiler 将调用静态 double log10(double x) 方法来查找相应值的以 10 为底的对数值。
package MathFunctions;
import java.util.ArrayList;
public class Log10MethodOnArrayList {
public static void main(String[] args) {
ArrayList<Double> logList = new ArrayList<Double>(5);
logList.add(1245.998);
logList.add(-1480.05);
logList.add(250.9876);
logList.add(170.01);
logList.add(2016.98);
logList.add(-8978.1898);
for (double x : logList) {
System.out.println(Math.log10(x));
}
}
}
3.0955173452206943
NaN
2.399652265765492
2.2304744673611583
3.304701591850494
NaN