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)就可以了。
转载请注明原文地址: https://ju.6miu.com/read-674807.html