Java对数

java对数

先看看Java源码里的对数函数(在Java.lang.Math里)

方法1:log()

  作用:返回以自然常数e为底数的对数值

说明:

  

e ≈ 2.71828 18284 59045 23536 02874 71352 66249 77572 47093 69995 95749 66967 62772 40766 30353 54759 45713 82178 52516 64274

 1 public static double log(double a) {
 2         return StrictMath.log(a); // default impl. delegates to StrictMath
 3     }
 4 
 5     /**
 6      * Returns the base 10 logarithm of a value.返回10为底的对数
 7      * 
 8      ***Special cases特别注意:
 9      *
10      * 1.If the argument is NaN or less than zero, then the result is NaN.非数或小于0返回NAN
11      * 2.If the argument is positive infinity, then the result is positive infinity. 正无穷,返回正无穷
12      * 3.If the argument is positive zero or negative zero, then the result is negative infinity. 参数是正负0,返回负无穷
13      * 4. If the argument is equal to 10 ^n for integer n, then the result is n. 
14      * 
15      */

 

方法2:log10()

作用:返回以10为底数的对数值

 1 public static double log10(double a) {
 2         return StrictMath.log10(a); // default impl. delegates to StrictMath
 3     }
 4 
 5 /** Returns the correctly rounded positive square root of a
 6      * value.
 7      * Special cases:
 8      * If the argument is NaN or less than zero, then the result is NaN.
 9      * If the argument is positive infinity, then the result is positive infinity.
10      * If the argument is positive zero or negative zero, then the
11      * result is the same as the argument.
12      * Otherwise, the result is the value closest to
13      * the true mathematical square root of the argument value.
14      * @return  the positive square root of .
15      *If the argument is NaN or less than zero, the result is NaN.
16      */
17 *翻译:
18 *特殊情况:
19 
20 *如果参数是NA或小于零,则结果是楠。
21 
22 *如果参数是正无穷大,则结果是正无穷大。
23 
24 *如果参数为正零或负零点,则
25 
26 *结果与论点相同。
27 
28 *否则,结果是最接近的值。
29 
30 *参数值的真正数学平方根。
31 
32 *@返回正平方根。
33 
34 *如果参数是NA或小于零,则结果是楠。
35 
36 */

 

如果不能理解特别注意的几条,看图:

对数

 

那么问题来了,怎样实现求任意底数的对数?

(注意:底数>0,底数不等于1)

先来看看换底公式:

log(a) b=log (c) b÷log (c) a ,即:

有了换底公式,我们就可以将任意其他底数换成jvm支持的底数e或10:

1 public class Logarithm {
2     public int log(double value, int base){
3         return  (int)(Math.log(value)/Math.log(base));//换成了底数e
4     }
5 }

 

转载于:https://www.cnblogs.com/XT-xutao/p/9977704.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值