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
0
投稿
猜你喜欢
- java中的线程状态🥇在操作系统层面,一个线程就两个状态:就绪和阻塞状态.但是java中为了在线程阻塞时能够更快速的知晓一个线程阻塞的原因,
- Java选择的泛型类型叫做类型擦除式泛型。什么是类型擦除式泛型呢?就是Java语言中的泛型只存在于程序源码之中,在编译后的字节码文件里,则全
- 将IDEA的默认快捷键设置 设置成为 Eclipse的默认快捷键设置题意有些拗口,但没关系,就是将idea中的快捷键转为自己刚学Java时使
- 问题:在用Java程序进行读写含中文的txt文件时,经常会出现读出或写入的内容会出现乱码。原因其实很简单,就是系统的编码和程序的编码采用了不
- 前言最近被问到了这个问题,第一次回答的也是很不好,在此参考网上答案进行整理记录。供大家学习参考。Synchronized修饰非静态方法Syn
- C# ping网络IP 实现网络状态检测的方法public string GetHostNameByIp(string&
- Object是所有类的父类,也就是说java中所有的类都是直接或者间接继承自Object类。比如你随便创建一个classA,虽然没有明说,但
- 今天没有继续学习新知识点,画出了一个随鼠标画出的 图形,知识点全都是之前学的,话不多说,先上图:代码部分: // ope
- 背景:日常开发ERP系统,会有一些工单或者合同之类需要填写打印。我们就会将其word模板来通过系统自动化填写并转换为PDF格式(PDF文件打
- Spring Bean的生命周期?首先说一下Servlet的生命周期:实例化,初始init,接收请求service,销毁destroy;Sp
- 本文实例为大家分享了Android实现支付宝支付密码输入界面的具体代码,供大家参考,具体内容如下效果图:主要代码:import java.u
- 一、项目简述功能:用户的邮箱注册、验证码验证以及用户登录。 不需要注册账号,也可以上传满足条件的临时文件,但是只4小时内有效。 文件的管理,
- 大家好,欢迎来到老胡的博客,今天我们继续了解设计模式中的职责链模式,这是一个比较简单的模式。跟往常一样,我们还是从一个真实世界的例子入手,这
- 本文实例为大家分享了C#实现XML文件读取的具体代码,供大家参考,具体内容如下using System.Collections;using
- 本文实例讲述了C# Console利用mspaint打开图像并保存的方法。分享给大家供大家参考,具体如下:调用画图板压缩图片System.D
- 在基于UI元素的自动化测试中, 无论是桌面的UI自动化测试,还是Web的UI自动化测试. 首先我们需要查找和识别UI
- 前言我们在开发Web应用时,肯定要为用户提供上传的功能,比如用户上传一张图像作为头像等。为了能上传文件,我们必须将表单的method设置为P
- 利用Javaweb开发的一个校园服务系统,通过发布自己的任务并设置悬赏金额,有些类似于赏金猎人,在这里分享给大家,有需要可以联系我:2186
- 一、实验题目二、分析实验要求为:实现一个界面,界面中包含一个文本显示区和两个按钮(存档和读档)读档按钮作用是打开文件并读取内容,将内容显示在
- GB2312是简体中文系统的标准编码 用“区” 跟“位”的概念表示 称之为区位码 区指代大的范围 位相当于偏移量。每个汉字占两个字节高位字节