• 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 }
  • 相关阅读:
    Python3 运算符
    Python之匿名函数
    Python之推导式、生成器表达式
    Python网络编程
    Python单例模式
    Python异常处理及元类
    Python面向对象(三)
    Python面向对象(二)
    Python面向对象(一)
    Python面向对象入门
  • 原文地址:https://www.cnblogs.com/XT-xutao/p/9977704.html
Copyright © 2020-2023  润新知