Java log10() 及示例


java.lang.Math.log10(double a) 返回雙精度值的以 10 為底的對數。特殊情況 -

  • 如果引數為 NaN 或小於零,則結果為 NaN。

  • 如果引數為正無窮大,則結果為正無窮大。

  • 如果引數為正零或負零,則結果為負無窮大。

  • 如果引數等於整數 n 的 10^n,則結果為 n。

示例

以下是一個在 Java 中實現 log10() 方法的示例 -

import java.lang.*;
public class MathDemo {
   public static void main(String[] args) {
      double x = 56567.5;
      double y = 100;
      // get the base 10 logarithm for x
      System.out.println("Math.log10(" + x + ")=" + Math.log10(x));
      // get the base 10 logarithm for y
      System.out.println("Math.log10(" + y + ")=" + Math.log10(y));
   }
}

輸出

Math.log10(56567.5)=4.752566985524987
Math.log10(100.0)=2.0

讓我們看另一個示例 -

示例

import java.lang.*;
public class MathDemo {
   public static void main(String[] args) {
      double x = -30;
      double y = 1.0 / 0; ;
      // get the base 10 logarithm for x
      System.out.println("Math.log10(" + x + ")=" + Math.log10(x));
      // get the base 10 logarithm for y
      System.out.println("Math.log10(" + y + ")=" + Math.log10(y));
   }
}

輸出

Math.log10(-30.0)=NaN
Math.log10(Infinity)=Infinity

更新日期: 2019 年 9 月 24 日

117 次瀏覽

開啟你的 職業之旅

完成課程獲得認證

開始學習
廣告
© . All rights reserved.