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
public static double log(double a) {
return StrictMath.log(a); // default impl. delegates to StrictMath
}
/**
* Returns the base 10 logarithm of a value.返回10为底的对数
*
***Special cases特别注意:
*
* 1.If the argument is NaN or less than zero, then the result is NaN.非数或小于0返回NAN
* 2.If the argument is positive infinity, then the result is positive infinity. 正无穷,返回正无穷
* 3.If the argument is positive zero or negative zero, then the result is negative infinity. 参数是正负0,返回负无穷
* 4. If the argument is equal to 10 ^n for integer n, then the result is n.
*
*/
方法2:log10()
作用:返回以10为底数的对数值
public static double log10(double a) {
return StrictMath.log10(a); // default impl. delegates to StrictMath
}
/** Returns the correctly rounded positive square root of a
* 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 the same as the argument.
* Otherwise, the result is the value closest to
* the true mathematical square root of the argument value.
* @return the positive square root of .
*If the argument is NaN or less than zero, the result is NaN.
*/
*翻译:
*特殊情况:
*如果参数是NA或小于零,则结果是楠。
*如果参数是正无穷大,则结果是正无穷大。
*如果参数为正零或负零点,则
*结果与论点相同。
*否则,结果是最接近的值。
*参数值的真正数学平方根。
*@返回正平方根。
*如果参数是NA或小于零,则结果是楠。
*/
如果不能理解特别注意的几条,看图:

那么问题来了,怎样实现求任意正底数的对数?
(注意:底数>0,底数不等于1)
先来看看换底公式:
log(a) b=log (c) b÷log (c) a ,即:

有了换底公式,我们就可以将任意其他底数换成jvm支持的底数e或10:
public class Logarithm {
public int log(double value, int base){
return (int)(Math.log(value)/Math.log(base));//换成了底数e
}
}
Java对数的更多相关文章
- java对数计算
Java对数函数的计算方法非常有问题,然而在API中却有惊人的误差.但是假如运用了以下的方法,用Java处理数字所碰到的小麻烦就可以轻而易举的解决了. Sun的J2SE提供了一个单一的Java对数方法 ...
- Java对数函数及Java对数运算
Java对数函数及Java对数运算 2010-05-17 10:32 中国IT实验室 佚名 关键字:Java Java对数函数的计算方法非常有问题,然而在API中却有惊人的误差.但是假如运用了 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 对数的操作 开始我的JAVA历程
package Text; public class Sumn { public static void main (String args[]){ System.out.println(" ...
- Java多线程 3 线程同步
在之前,已经学习到了线程的创建和状态控制,但是每个线程之间几乎都没有什么太大的联系.可是有的时候,可能存在多个线程多同一个数据进行操作,这样,可能就会引用各种奇怪的问题.现在就来学习多线程对数据访问的 ...
- 八大排序算法Java
目录(?)[-] 概述 插入排序直接插入排序Straight Insertion Sort 插入排序希尔排序Shells Sort 选择排序简单选择排序Simple Selection Sort 选择 ...
- 数据结构Java实现01----算法概述
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- Java面试宝典系列之基础排序算法
本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...
- Java经典兔子问题
题目:古典问题:3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 分析:首先我们要明白题目的意思指的是每个月的兔子总对数:假设将兔子分为小 ...
随机推荐
- json日期字符串格式化时间
var str = '/Date(1333245600000+0800)/'; function data_string(str) { var d = eval('new ' + str. ...
- Left join update和 ROW_NUMBER
(1)Left join 更新update a set a.name=b.name from a left join b on a.id=b.stuid(2)ROW_NUMBERselect ROW_ ...
- jquery实现图片上传前的预览
html代码 <div id="uploadPreview"></div> <input id="uploadImage" typ ...
- VM4061 layui.js:2 Layui hint: form is not a valid module
报错:VM4061 layui.js:2 Layui hint: form is not a valid module 解决办法:当你遇到类似这样报错,说某某某不是一个有效的模块时,不防在layui. ...
- CSS面试复习(一):HTML强化
1. HTML常见元素和理解 head类 meta:字符集.base:路径. a[href,target] img[src,alt] table td[colspan,rowspan] form[ta ...
- 【webpack】使用DllPlugin拆分模块
开发过程中,我们经常需要引入大量第三方库,这些库并不需要随时修改或调试,我们可以使用DllPlugin和DllReferencePlugin单独构建它们. 具体使用如下: const HtmlWebp ...
- SpringBoot - 资源文件配置读取
Examp1:读取核心配置文件信息application.properties的内容 方法一:使用@Value方式(常用) 1.application.properties中自定义参数 test.ms ...
- 小程序开发-Step1
先申请一个小程序 https://mp.weixin.qq.com/wxopen/waregister?action=step1 根据以上链接步骤一步一步来,认识字就可以完成,没什么特殊的 申请成功之 ...
- Netty源码学习笔记
1.ByteBuf
- Python学习-环境搭建(IronPython)
一.IDE环境 VS2013 下安装 PTVS 2.2.2 VS 2013 VS2015 已经集成了Python的开发环境 二.安装 IronPython 下载地址:http://ironpytho ...