Java给我提供的数学计算的工具类Math计算对数的函数有两个:

/**

* Returns the natural logarithm (base e) of a {@code double}

* value. Special cases:

*

  • If the argument is NaN or less than zero, then the result

    * is NaN.

    *

  • If the argument is positive infinity, then the result is

    * positive infinity.

    *

  • If the argument is positive zero or negative zero, then the

    * result is negative infinity.

*

*

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:

*

*

  • If the argument is NaN or less than zero, then the result

    * is NaN.

    *

  • If the argument is positive infinity, then the result is

    * positive infinity.

    *

  • If the argument is positive zero or negative zero, then the

    * result is negative infinity.

    *

  • If the argument is equal to 10n for

    * integer n, then the result is n.

    *

*

*

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的log计算_Java普通对数(log)计算方法相关推荐

  1. java gps 距离计算_Java教程之地图中计算两个GPS坐标点的距离

    原标题:Java教程之地图中计算两个GPS坐标点的距离 在日常开发中,我们难免要计算两个左边之间的距离,但是地图软件api的接口普遍要求我们必须要先将坐标点传递到他们服务器,然后计算出一个距离返还给我 ...

  2. java长方形周长计算_Java练习 SDUT-3339_计算长方形的周长和面积(类和对象)

    ###计算长方形的周长和面积(类和对象) Time Limit: 1000 ms Memory Limit: 65536 KiB ####Problem Description 设计一个长方形类Rec ...

  3. java汽车油耗计算_Java程序设计单元4任务1油耗计算程序设计.ppt

    5. 任务实训 一.实训目的 熟悉JavaGUI编程的基本方法和步骤. 二.实训内容 在窗口中输入整数n,计算输出n的3次方. 5. 任务实训 三.简要提示 JFrame实现窗口,JTextField ...

  4. java 中时间计算_java中关于时间的计算

    1.描述:在原有时间上增加一个时间差: 代码示例: Date psd = sysDate(); String workTimeP1 = mapP.get("WORK_TIME_") ...

  5. java excel公式计算_java poi读取excel公式,返回计算值(转) | 学步园

    http://blog.csdn.net/CYZERO/article/details/6573015 经测试,确实可以 1 package hrds.zpf.poi; 2 3  import org ...

  6. java汽车油耗计算_JAVA面向对象编程-试卷B

    JAVA面向对象编程-试卷B 所属分类:其他 开发工具:Java 文件大小:15KB 下载次数:1 上传日期:2017-11-10 20:39:45 上 传 者:弑神唐三 说明:  定义一个交通工具类 ...

  7. java gps 距离计算_java计算两个GPS经纬度之间的距离(转)

    /** * Created by yuliang on 2015/3/20. */ public class LocationUtils { private static double EARTH_R ...

  8. java心电图心率计算_java如何画心电图?

    匿名用户 1级 2016-10-08 回答 电数据图的代码: using System; using System.Collections.Generic; using System.Componen ...

  9. java calendar日期计算_JAVA中用CALENDAR类计算周和周的起始日期(转)

    1 packagecom.tongyue.hot.web;2 3 importjava.util.Calendar;4 importjava.util.Date;5 importjava.util.G ...

  10. java 个税计算_java写个人所得税

    向我提问 杨扬海律师 解答问题:560条 |好评:17个 依法纳税是每个公民的义务,如果不缴纳个税肯定是要承担责任的.到底不缴纳个税的后果: 一.凡是年所得额超过12万元的公民不依法自行申报缴纳个人所 ...

最新文章

  1. 在java下使用log4j2记录日志
  2. ActiveX组件及其注册 (轉)
  3. JSON 使用 教程
  4. 论文浅尝 | Rot-Pro:通过知识图谱嵌入中的投影建模关系的传递性
  5. 中移4G模块-ML302-OpenCpu开发-PCF8591测量电压
  6. 记坑 ----- Arrays.sort()
  7. 逼真照片随手画,马良神笔已上线 | 点击收获这份英伟达GauGAN开源代码
  8. 使用 mod_rewrite 来修改 Confluence 6 的 URLs
  9. C语言 16进制转float
  10. java开发冒险岛系统实训报告_2017级C语言大作业 - 小小冒险岛
  11. 一种在BIOS中嵌入应用程序的方法及实现
  12. 《创业时,我们在知乎聊什么》- 书摘
  13. 视觉SLAM十四讲第三讲
  14. qrc路径_C语言 在Qt中获取qrc文件的路径
  15. 笔记木计算机自动关机怎么办,为什么笔记本电脑自动关机 笔记本电脑自动关机解决方法...
  16. 手机水星路由器服务器无响应,水星路由器无线wifi连接成功但上不了网的解决方法...
  17. 世界十大著名黑客 居然还有苹果创始人!
  18. CleanMyMac X 4.8版本更新!
  19. mac 破解安装 navicat
  20. 动态调试之——x64dbg的使用

热门文章

  1. 解读国密非对称加密算法SM2
  2. matlab besselh,关于用matlab求bessel函数零点
  3. 《四维全息算法》第六讲--随机、布朗运动、随机游走、混沌、分形混沌与时序拟合分析之间的关系
  4. 购买嵌入式硬件,焊接设备避坑指南/自用工具推荐
  5. android_5.0简介
  6. nuxt使用videojs播放flv格式视频
  7. 金山PDF转WOED 1.3.0.1007版本
  8. 计算机科学导论佛罗赞第4版,计算机科学导论 原书第4版
  9. pthread_attr_setinheritsched,pthread_attr_setschedparam函数详解
  10. 中国移动推自有品牌终端利大于弊