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个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 分析:首先我们要明白题目的意思指的是每个月的兔子总对数:假设将兔子分为小 ...
随机推荐
- IScroll5要防止重复加载
增加一个判断条件,ajax未返回前,设置为true,返回前设置为false,只有为false下才能够出发加载数据事件效果好很多.
- 【核心】project(idea文件)、module(iml文件)到SSM集成、热部署、Tomcat启动、MAVEN依赖冲突
http://wiki.jikexueyuan.com/project/intellij-idea-tutorial/project-composition-introduce.html 在 Inte ...
- 【noip 2016】提高组
D1T1.玩具谜题 题目链接 直接模拟就好了……water. #include<cstdio> int n,m,a,s,ans; ];]; int main() { scanf(" ...
- 深度学习 ——style reconstruction
多层神经网络的实质就是为了找出更复杂,更内在的features...图像的style, how to describe, impossible! 但是人眼却可以分辨. (参考论文 A Neural a ...
- [C++]2-1 水仙花数
/* 水仙花数 输出100-999中的所有水仙花数.若三位数ABC满足ABC=A^3+B^3+C^3,则 称其为水仙花数.例如:153 = 1^3 + 5^3 + 3^3,故153是水仙花数. */ ...
- MacOS下安装小米SQL优化工具soar
1 下载源码包 赋予权限 wget https://github.com/XiaoMi/soar/releases/download/0.11.0/soar.darwin-amd64 -O soar ...
- Nginx(二) nginx 无法启动
有时候在客户端输入:nginx 但是终端会输出以下,显示启动失败 nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already i ...
- light oj 1011 - Marriage Ceremonies
题目大意: 给出n*n的矩阵Map,Map[i][j]代表第i个男人和第j个女人之间的满意度,求男女一一配对后,最大的满意度之和. 题目思路:状态压缩 题目可看做每行取一点,所有点不同列的情况下,各个 ...
- linux下cmake安装mysql 源码
1.假设已经有mysql-5.6.21.tar.gz以及cmake-2.8.4.tar.gz两个源文件 (1)先安装cmake(mysql5.5以后是通过cmake来编译的) [root@ rhel5 ...
- ZKClient操作zookeeper
前面简单研究了curator的使用,下面简单研究zkclient的使用. 1.创建连接 private static final String CONNECT_ADDR = "127.0.0 ...