溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java 中怎么實現對數和指數計算

發(fā)布時間:2021-08-13 14:28:15 來源:億速云 閱讀:358 作者:Leah 欄目:開發(fā)技術

本篇文章為大家展示了java 中怎么實現對數和指數計算,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。


java計算對數和指數

public static void main(String[] args) throws InterruptedException{
    int a = 10;
    int b = 1000000;
    System.out.println(getlog(b,a));
   
}
static double getlog(int b,int a){
   return Math.log(b)/Math.log(a);
}

Math提供了一個自然底數的方法,Math.log(),自定義方法,但是運行結果精度會丟失。

運行結果為5.99999

 public static void main(String[] args) throws InterruptedException{
        BigDecimal a = new BigDecimal(10);
        BigDecimal b = new BigDecimal(1000000);
        System.out.println(getlog(b,a));
//
    }
    static double getlog(BigDecimal b, BigDecimal a){
       return Math.log(b.doubleValue())/Math.log(a.doubleValue());
    }

結果為6.0

精度出問題就找BigDecimal 就可以了。

指數的話,直接使用Math.pow(a,b)就可以了。

Java普通對數(log)計算

Java給我提供的數學計算的工具類Math計算對數的函數有兩個:

/**
     * Returns the natural logarithm (base <i>e</i>) of a {@code double}
     * value.  Special cases:
     * <ul><li>If the argument is NaN or less than zero, then the result
     * is NaN.
     * <li>If the argument is positive infinity, then the result is
     * positive infinity.
     * <li>If the argument is positive zero or negative zero, then the
     * result is negative infinity.</ul>
     *
     * <p>The computed result must be within 1 ulp of the exact result.
     * Results must be semi-monotonic.
     *
     * @param   a   a value
     * @return  the value ln&nbsp;{@code a}, the natural logarithm of
     *          {@code a}.
     */
    public static double log(double a) {
        return StrictMath.log(a); // default impl. delegates to StrictMath
    }
 
    /**
     * Returns the base 10 logarithm of a {@code double} value.
     * Special cases:
     *
     * <ul><li>If the argument is NaN or less than zero, then the result
     * is NaN.
     * <li>If the argument is positive infinity, then the result is
     * positive infinity.
     * <li>If the argument is positive zero or negative zero, then the
     * result is negative infinity.
     * <li> If the argument is equal to 10<sup><i>n</i></sup> for
     * integer <i>n</i>, then the result is <i>n</i>.
     * </ul>
     *
     * <p>The computed result must be within 1 ulp of the exact result.
     * Results must be semi-monotonic.
     *
     * @param   a   a value
     * @return  the base 10 logarithm of  {@code a}.
     * @since 1.5
     */
    public static double log10(double a) {
        return StrictMath.log10(a); // default impl. delegates to StrictMath
    }

log(double a),log10(double a)從源碼doc注釋我們可以看到分別是計算自然對數和以10為底的對數。

如下代碼:

double x = Math.log(10);

等價于:x = ln10 或 x = loge(10),即以e為底的自然對數。

問題來了,如果我們要計算非常規(guī)底數的對數怎么辦呢?比如我們要計算以33為底27的對數(也就是33的多少次方運算結果為27)?

這個就需要使用數學的換底公式:logx(y)=ln(y)/ln(x);

代碼實現以x為底y的對數計算工具類:

public class Logarithm {
    public static double log(double value, double base) {
        return Math.log(value) / Math.log(base);
    }
}

這樣我們計算以33為底27的對數:

public static void main(String[] args) {
        double log = log(27, 33);
        System.out.println(log);
    }
 
    private static double log(double value, double base) {
        return Logarithm.log(value) / Math.log(base);
    }

計算結果:0.9426082478202944

本demo使用log以及換底公式,也可以使用log10和換底公式計算,結果是一樣的。

如:

public static double log(double value, double base) {
        return Math.log10(value) / Math.log10(base);
}

普通底對數計算的關鍵點在于使用換底公式轉換為工具類提供的特殊對數進行計算即可。

上述內容就是java 中怎么實現對數和指數計算,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI