欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 對(duì)數(shù)和指數(shù)計(jì)算方式

 更新時(shí)間:2021年08月13日 11:17:15   作者:Dawn_Bells  
這篇文章主要介紹了java 對(duì)數(shù)和指數(shù)計(jì)算方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

java計(jì)算對(duì)數(shù)和指數(shù)

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提供了一個(gè)自然底數(shù)的方法,Math.log(),自定義方法,但是運(yùn)行結(jié)果精度會(huì)丟失。

運(yùn)行結(jié)果為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());
    }

結(jié)果為6.0

精度出問(wèn)題就找BigDecimal 就可以了。

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

Java普通對(duì)數(shù)(log)計(jì)算

Java給我提供的數(shù)學(xué)計(jì)算的工具類(lèi)Math計(jì)算對(duì)數(shù)的函數(shù)有兩個(gè):

    /**
     * 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注釋我們可以看到分別是計(jì)算自然對(duì)數(shù)和以10為底的對(duì)數(shù)。

如下代碼:

double x = Math.log(10);

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

問(wèn)題來(lái)了,如果我們要計(jì)算非常規(guī)底數(shù)的對(duì)數(shù)怎么辦呢?比如我們要計(jì)算以33為底27的對(duì)數(shù)(也就是33的多少次方運(yùn)算結(jié)果為27)?

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

代碼實(shí)現(xiàn)以x為底y的對(duì)數(shù)計(jì)算工具類(lèi):

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

這樣我們計(jì)算以33為底27的對(duì)數(shù):

    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);
    }

計(jì)算結(jié)果:0.9426082478202944

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

如:

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

普通底對(duì)數(shù)計(jì)算的關(guān)鍵點(diǎn)在于使用換底公式轉(zhuǎn)換為工具類(lèi)提供的特殊對(duì)數(shù)進(jìn)行計(jì)算即可。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring jpa和mybatis整合遇到的問(wèn)題解析

    Spring jpa和mybatis整合遇到的問(wèn)題解析

    有朋友說(shuō)jpa相比mybatis太難用,多表聯(lián)合的查詢(xún)寫(xiě)起來(lái)也比較費(fèi)勁,所以便加入了mybatis的支持,在配置jpa時(shí)遇到各種問(wèn)題,需要修改相關(guān)配置文件,下面小編給大家分享下修改配置文件的思路,感興趣的朋友參考下
    2016-10-10
  • 淺談Java獲得多線(xiàn)程的返回結(jié)果方式(3種)

    淺談Java獲得多線(xiàn)程的返回結(jié)果方式(3種)

    這篇文章主要介紹了淺談Java獲得多線(xiàn)程的返回結(jié)果方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • ElasticSearch?深度分頁(yè)示例解析

    ElasticSearch?深度分頁(yè)示例解析

    這篇文章主要為大家介紹了ElasticSearch?深度分頁(yè)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Mybatis-Plus自動(dòng)填充的實(shí)現(xiàn)示例

    Mybatis-Plus自動(dòng)填充的實(shí)現(xiàn)示例

    這篇文章主要介紹了Mybatis-Plus自動(dòng)填充的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • java的arrays數(shù)組排序示例分享

    java的arrays數(shù)組排序示例分享

    排序算法,基本的高級(jí)語(yǔ)言都有一些提供。C語(yǔ)言有qsort()函數(shù),C++有sort()函數(shù),java語(yǔ)言有Arrays類(lèi)(不是Array)。用這些排序時(shí),都可以寫(xiě)自己的排序規(guī)則
    2014-02-02
  • Java攔截器和過(guò)濾器的區(qū)別分析

    Java攔截器和過(guò)濾器的區(qū)別分析

    今天帶大家分析java攔截器和過(guò)濾器的區(qū)別,文中有非常詳細(xì)的解釋說(shuō)明,對(duì)正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • 數(shù)據(jù)庫(kù)連接池c3p0配置_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    數(shù)據(jù)庫(kù)連接池c3p0配置_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了數(shù)據(jù)庫(kù)連接池c3p0配置的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • java中Timer定時(shí)器的使用和啟動(dòng)方式

    java中Timer定時(shí)器的使用和啟動(dòng)方式

    這篇文章主要介紹了java中Timer定時(shí)器的使用和啟動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 利用Spring?Boot和JPA創(chuàng)建GraphQL?API

    利用Spring?Boot和JPA創(chuàng)建GraphQL?API

    這篇文章主要介紹了利用Spring?Boot和JPA創(chuàng)建GraphQL?API,GraphQL既是API查詢(xún)語(yǔ)言,也是使用當(dāng)前數(shù)據(jù)執(zhí)行這些查詢(xún)的運(yùn)行時(shí),下文更多相關(guān)內(nèi)容介紹需要的小伙伴可以參考一下
    2022-04-04
  • 淺談Thread.sleep()為什么要拋出中斷異常

    淺談Thread.sleep()為什么要拋出中斷異常

    本文主要介紹了淺談Thread.sleep()為什么要拋出中斷異常,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評(píng)論