java 对数和指数计算方式
作者:Dawn_Bells 时间:2022-08-27 16:50:07
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 {@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);
}
普通底对数计算的关键点在于使用换底公式转换为工具类提供的特殊对数进行计算即可。
来源:https://blog.csdn.net/Dawn_Bells/article/details/70172134
标签:java,对数,指数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot修改启动端口的方法
2022-02-10 05:49:55
Java中List使用stream流转成map的几种方式详解
2022-07-31 21:46:12
SpringBoot使用Jackson配置全局时间日期格式
2021-10-04 12:45:06
![](https://img.aspxhome.com/file/2023/0/83090_0s.png)
java开源项目jeecgboot的超详细解析
2023-07-19 03:30:53
![](https://img.aspxhome.com/file/2023/7/71677_0s.png)
Java基础之多线程
2022-11-30 11:01:43
Java中计算时间差的方法
2023-11-15 10:35:44
全面理解java中的异常处理机制
2023-10-26 04:08:20
基于javaMybatis存进时间戳的问题
2023-11-29 02:55:51
解决grails服务端口冲突的办法(grails修改端口号)
2023-09-12 01:00:03
基于C语言string函数的详解
2023-06-28 05:33:25
Springboot和bootstrap实现shiro权限控制配置过程
2022-01-19 21:31:23
![](https://img.aspxhome.com/file/2023/2/69952_0s.png)
详解java中BigDecimal精度问题
2021-08-17 10:24:59
![](https://img.aspxhome.com/file/2023/9/64039_0s.jpg)
Java算法之时间复杂度和空间复杂度的概念和计算
2023-06-11 17:47:56
![](https://img.aspxhome.com/file/2023/9/79519_0s.png)
详解Java中的线程池
2023-11-10 16:33:27
![](https://img.aspxhome.com/file/2023/9/59369_0s.png)
一篇文章带你深入了解Java线程池
2021-11-22 21:59:42
c# 图片加密解密的实例代码
2023-08-20 21:21:01
Java如何实现定时任务
2021-11-07 02:57:38
mybatis注解与xml常用语句汇总
2022-05-17 18:39:47
java8实现List中对象属性的去重方法
2023-08-30 20:50:48
浅谈C#中对引用类型的误解
2021-10-18 12:40:37
![](https://img.aspxhome.com/file/2023/4/74844_0s.png)