最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

運行結(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

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

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

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

Java給我提供的數(shù)學(xué)計算的工具類Math計算對數(shù)的函數(shù)有兩個:

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

如下代碼:

double x = Math.log(10);

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

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

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

代碼實現(xiàn)以x為底y的對數(shù)計算工具類:

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

這樣我們計算以33為底27的對數(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);
    }

計算結(jié)果:0.9426082478202944

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

如:

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

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

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

相關(guān)文章

  • java?Map.Entry的使用示例

    java?Map.Entry的使用示例

    Map.Entry是Java中Map接口的嵌套接口,它提供了獲取鍵和值的方法及遍歷和操作Map的鍵值對,本文就來詳細(xì)的介紹一下,感興趣的可以了解一下
    2024-11-11
  • java如何通過Kerberos認(rèn)證方式連接hive

    java如何通過Kerberos認(rèn)證方式連接hive

    該文主要介紹了如何在數(shù)據(jù)源管理功能中適配不同數(shù)據(jù)源(如MySQL、PostgreSQL和Hive),特別是如何在SpringBoot3框架下通過Kerberos認(rèn)證與Hive進行安全交互,文章詳細(xì)描述了Kerberos認(rèn)證過程,包括配置krb5.conf和keytab文件、處理Hadoop和Hive版本兼容性問題
    2025-02-02
  • Ehcache簡介_動力節(jié)點Java學(xué)院整理

    Ehcache簡介_動力節(jié)點Java學(xué)院整理

    這篇文章主要介紹了Ehcache簡介,使用Spring的AOP進行整合,可以靈活的對方法的返回結(jié)果對象進行緩存
    2017-07-07
  • java 如何將多種字符串格式 解析為Date格式

    java 如何將多種字符串格式 解析為Date格式

    這篇文章主要介紹了java 如何將多種字符串格式 解析為Date格式的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java 多態(tài)中繼承的轉(zhuǎn)型詳解與用法分析

    Java 多態(tài)中繼承的轉(zhuǎn)型詳解與用法分析

    繼承是java面向?qū)ο缶幊碳夹g(shù)的一塊基石,因為它允許創(chuàng)建分等級層次的類。繼承就是子類繼承父類的特征和行為,使得子類對象(實例)具有父類的實例域和方法,或子類從父類繼承方法,使得子類具有父類相同的行為
    2021-10-10
  • Java中Cookie和Session詳解及區(qū)別總結(jié)

    Java中Cookie和Session詳解及區(qū)別總結(jié)

    這篇文章主要介紹了Java中Cookie和Session詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-06-06
  • 字節(jié)二面SpringBoot可以同時處理多少請求

    字節(jié)二面SpringBoot可以同時處理多少請求

    這篇文章主要為大家介紹了字節(jié)二面之SpringBoot可以同時處理多少請求面試分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • SpringBoot測試之高級配置方式

    SpringBoot測試之高級配置方式

    這篇文章主要介紹了SpringBoot測試之高級配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 簡單了解Spring Boot及idea整合jsp過程解析

    簡單了解Spring Boot及idea整合jsp過程解析

    這篇文章主要介紹了簡單了解Spring Boot及idea整合jsp過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • Java Serializable和Parcelable詳解及實例代碼

    Java Serializable和Parcelable詳解及實例代碼

    這篇文章主要介紹了Java Serializable和Parcelable詳解,并附實例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-09-09

最新評論

青浦区| 威远县| 开远市| 仁化县| 大城县| 仙桃市| 江永县| 怀集县| 扶绥县| 十堰市| 佛冈县| 东源县| 清丰县| 定远县| 阿鲁科尔沁旗| 汉阴县| 聂拉木县| 普洱| 神木县| 清涧县| 莒南县| 天镇县| 白河县| 霍山县| 敦煌市| 延安市| 固始县| 邮箱| 平果县| 陈巴尔虎旗| 金昌市| 嘉定区| 馆陶县| 资阳市| 繁昌县| 红桥区| 枣阳市| 彰化市| 曲阜市| 子洲县| 阿克|