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个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 分析:首先我们要明白题目的意思指的是每个月的兔子总对数:假设将兔子分为小 ...
随机推荐
- html取消回车刷新提交
<form class="weui-search-bar__form" onsubmit="return false;"> <form cla ...
- rabbitMQ学习1:消息队列介绍与rabbitmq安装使用
1. 什么是消息队列 生活里的消息队列,如同邮局的邮箱, 如果没邮箱的话, 邮件必须找到邮件那个人,递给他,才玩完成,那这个任务会处理的很麻烦,很慢,效率很低 但是如果有了邮箱, 邮件直接丢给邮箱,用 ...
- cygwin 安装包管理器 apt-cyg
https://github.com/transcode-open/apt-cyg apt-cyg is a simple script. To install: lynx -source https ...
- js原生事件
js原生事件封装 // 事件处理对象 var EventUtil = { // 添加事件监听 add: function(element, type, callback){ if(element.ad ...
- Spring Cloud学习资料
博客 1.跟我学Spring Cloud 2.周立|Spring Cloud 3.Spring Cloud基础教程(强烈推荐) 4.Spring Cloud系列文章 5.forezp|史上最简单的 S ...
- C - CodeCoder vs TopForces Gym - 101142C (连通块+思维)
题目链接: C - CodeCoder vs TopForces Gym - 101142C 题目大意:给你n个人的信息,每一个人的信息包括两个.t1和t2.A>B的前提是A的t1和t2至少有一 ...
- Setup Post-mission Camera
编辑BP_GameMode 新建Actor命名为BP_SpectatingViewpoint 添加摄像机 将摄像机调整到合适的位置,右键选择 调整到合适的位置,点击这个按钮,退出驾驶模式 摄像机已经移 ...
- dom解析xml随笔
1.dom解析jar包准备: dom解析需用到dom4j的jar包,比如我在项目中用到的的是dom4j-1.6.1jar,因为项目用的是MAVEN,所以可直接到maven中央库去搜索相关pom坐标配置 ...
- 2017 ACM/ICPC(北京)总结
这个季节的,北京真的很冷. 下午的热身赛,我依然先去敲一道搜索题,但是很不幸这道搜索题坑点还是蛮多的,浪费了好长时间后依然没能A掉,期间Codeblocks崩溃一次使得代码完全丢失,在队友的建议下便暂 ...
- Zookeeper学习笔记2
环境搭建 JDK(>1.6)安装 略 Zookeeper安装 1.下载 http://mirror.bit.edu.cn/apache/zookeeper/stable/zookeeper-3. ...