java 对数和指数计算方式
作者:Dawn_Bells 发布时间:2022-08-27 16:50:07
标签:java,对数,指数
java计算对数和指数
public static void main(String[] args) throws InterruptedException{
int a = 10;
int b = 1000000;
System.out.println(getlog(b,a));
}
static double getlog(int b,int a){
return Math.log(b)/Math.log(a);
}
Math提供了一个自然底数的方法,Math.log(),自定义方法,但是运行结果精度会丢失。
运行结果为5.99999
public static void main(String[] args) throws InterruptedException{
BigDecimal a = new BigDecimal(10);
BigDecimal b = new BigDecimal(1000000);
System.out.println(getlog(b,a));
//
}
static double getlog(BigDecimal b, BigDecimal a){
return Math.log(b.doubleValue())/Math.log(a.doubleValue());
}
结果为6.0
精度出问题就找BigDecimal 就可以了。
指数的话,直接使用Math.pow(a,b)就可以了。
Java普通对数(log)计算
Java给我提供的数学计算的工具类Math计算对数的函数有两个:
/**
* Returns the natural logarithm (base <i>e</i>) of a {@code double}
* value. Special cases:
* <ul><li>If the argument is NaN or less than zero, then the result
* is NaN.
* <li>If the argument is positive infinity, then the result is
* positive infinity.
* <li>If the argument is positive zero or negative zero, then the
* result is negative infinity.</ul>
*
* <p>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:
*
* <ul><li>If the argument is NaN or less than zero, then the result
* is NaN.
* <li>If the argument is positive infinity, then the result is
* positive infinity.
* <li>If the argument is positive zero or negative zero, then the
* result is negative infinity.
* <li> If the argument is equal to 10<sup><i>n</i></sup> for
* integer <i>n</i>, then the result is <i>n</i>.
* </ul>
*
* <p>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);
}
普通底对数计算的关键点在于使用换底公式转换为工具类提供的特殊对数进行计算即可。
来源:https://blog.csdn.net/Dawn_Bells/article/details/70172134


猜你喜欢
- static :静态常量,静态方法,静态代码块静态变量: 静态变量属于类的,使用类名来访问,非静态变量是属于对象的,"必须&quo
- 1,现在因为遇到一个读取pdf文件文本信息遇到乱么问题,才找到这个文本字符串的编码转换的实现方式来判断是否存在乱码(0>乱码>2
- Android开发,触控无处不在。对于一些 不咋看源码的同学来说,多少对这块都会有一些疑惑。View事件的分发机制,不仅在做业务需求中会碰到
- List查询JAVA中从数据库中取数据,根据MyBits返回结果主要有两种类型的List,一种是List<Entity>,还一种
- C++编写的一个图书管理系统,供大家参考,具体内容如下2018大一的课设,搬到这纪念一下,共1200多行代码为图书管理人员编写一个图书管理系
- spring boot是个好东西,可以不用容器直接在main方法中启动,而且无需配置文件,方便快速搭建环境。可是当我们要同时启动2个spri
- 显示一个计时器开始计时,当计时器到达15s的时候,停止计时。此时页面多一个重置按钮,可再次进行计时。页面布局<LinearLayout
- 背景前段时间同事碰到一个问题,需要在 SpringCloud 的 Feign 调用中使用自定义的 URL;通常情况下是没有这个需求的;毕竟都
- Java 15 在 2020 年 9 月发布,虽然不是长久支持版本,但是也带来了 14 个新功能,这些新功能中有不少是十分实用的。Java
- java中多种方式读文件 一、多种方式读文件内容。 1、按字节读取文件内容 2、按字符读取文件内容 3、按行读取文件内容 4、随机读取文件内
- 一、前言今天我们来说一说 Spring Bean 的生命周期,小伙伴们应该在面试中经常遇到,这是正常现象。因为 Spring Bean 的生
- ava最明显的一个优势就是它的内存管理机制。你只需简单创建对象,java的垃圾回收机制负责分配和释放内存。然而情况并不像想像的那么简单,因为
- 开始研究android开发,搭建开发环境的时候就出了问题……果然是好事多磨~ 安装了jdk,配置环境变量,安装了完整版的adt、创建了hel
- Process#waitFor()阻塞问题有时需要在程序中调用可执行程序或脚本命令:Process process = Runtime.ge
- 有很多地方要用到DatePickerDialog。但有时项目用到的主题样式是很丑的样式,显示出来的真丑。而我们真正想要的样式是这样的。这个就
- 建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创
- TCP/IP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket,从而在通信的两端之间形成网络虚拟链路。一旦建立了虚拟的网络链
- 起因最近在写CRUD的时候,发现有个分页的VO写的健壮性比较差,一时手痒改了一下,没想到改了之后好几个功能都出现了问题。原VO关键代码如下:
- Java本身都是值传递式的调用,对于对象传递的是地址值。给地址值重新赋值等于重新指向,不会影响外层。而且这里Integer对象也有特殊性。其
- 在java中类之间也是有着继承关系的,就我们之前有提到不少父类与子类的一些问题。 讲的以子类的调用为主,那么有小伙伴知道父类的调用方法吗?这