java 对数和指数计算方式

 更新时间:2021年08月13日 11:17:15   作者:Dawn_Bells  
这篇文章主要介绍了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为底的自然对数。

问题来了,如果我们要计算非常规底数的对数怎么办呢?比如我们要计算以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 Map.Entry的使用示例

    java Map.Entry的使用示例

    Map.Entry是Java中Map接口的嵌套接口,它提供了获取键和值的方法及遍历和操作Map的键值对,本文就来详细的介绍一下,感兴趣的可以了解一下
    2024-11-11
  • java如何通过Kerberos认证方式连接hive

    java如何通过Kerberos认证方式连接hive

    该文主要介绍了如何在数据源管理功能中适配不同数据源(如MySQL、PostgreSQL和Hive),特别是如何在SpringBoot3框架下通过Kerberos认证与Hive进行安全交互,文章详细描述了Kerberos认证过程,包括配置krb5.conf和keytab文件、处理Hadoop和Hive版本兼容性问题
    2025-02-02
  • Ehcache简介_动力节点Java学院整理

    Ehcache简介_动力节点Java学院整理

    这篇文章主要介绍了Ehcache简介,使用Spring的AOP进行整合,可以灵活的对方法的返回结果对象进行缓存
    2017-07-07
  • java 如何将多种字符串格式 解析为Date格式

    java 如何将多种字符串格式 解析为Date格式

    这篇文章主要介绍了java 如何将多种字符串格式 解析为Date格式的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java 多态中继承的转型详解与用法分析

    Java 多态中继承的转型详解与用法分析

    继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类。继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为
    2021-10-10
  • Java中Cookie和Session详解及区别总结

    Java中Cookie和Session详解及区别总结

    这篇文章主要介绍了Java中Cookie和Session详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
    2022-06-06
  • 字节二面SpringBoot可以同时处理多少请求

    字节二面SpringBoot可以同时处理多少请求

    这篇文章主要为大家介绍了字节二面之SpringBoot可以同时处理多少请求面试分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • SpringBoot测试之高级配置方式

    SpringBoot测试之高级配置方式

    这篇文章主要介绍了SpringBoot测试之高级配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • 简单了解Spring Boot及idea整合jsp过程解析

    简单了解Spring Boot及idea整合jsp过程解析

    这篇文章主要介绍了简单了解Spring Boot及idea整合jsp过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • Java Serializable和Parcelable详解及实例代码

    Java Serializable和Parcelable详解及实例代码

    这篇文章主要介绍了Java Serializable和Parcelable详解,并附实例代码的相关资料,需要的朋友可以参考下
    2016-09-09

最新评论