在 Java 中计算对数
本文将介绍如何使用 Math.log
函数在 Java 中计算对数。
在 Java 中使用 Math.log
计算给定值的对数
在 Java 中,java.lang.Math
库具有函数 Math.log()
来计算给定值的对数。输入值可以是双精度、整数或浮点数,并返回双精度值。
我们必须确保数字不是负数、零或无穷大; 否则,输出将不是双精度数据类型。
下面的例子展示了 Java 中 Math.log
的使用。
import java.lang.Math;
class Java_Log {
public static void main(String args[])
{
double p = -4.3;
double q = 6.0 / 0;
double r = 0;
double s = 130.333;
double u = 130.333/30;
int v = 5;
float w = 34;
// The negative double in the Math.log function will output: NaN
System.out.println("The Output for Negative Integer:");
System.out.println(Math.log(p));
// The positive infinity in the Math.log function will output: Infinity
System.out.println("The Output for Positive Infinity:");
System.out.println(Math.log(q));
// The positive zero in the Math.log function will output: - Infinity
System.out.println("The Output for Zero:");
System.out.println(Math.log(r));
//The positive double argument in the Math.log function will output: logarithm answer
System.out.println("The Output for positive double:");
System.out.println(Math.log(s));
//The positive double argument in the Math.log function will output: logarithm answer
System.out.println("The Output for Positive double in division form:");
System.out.println(Math.log(u));
//The positive integer argument in the Math.log function will output: logarithm answer
System.out.println("The Output for Positive Integer:");
System.out.println(Math.log(v));
//The positive integer float argument in the Math.log function will output: logarithm answer
System.out.println("The Output for Positive float:");
System.out.println(Math.log(w));
}
}
输出:
The Output for Negative Integer:
NaN
The Output for Positive Infinity:
Infinity
The Output for Zero:
-Infinity
The Output for positive double:
4.870092713769228
The Output for Positive double in division form:
1.468895332107073
The Output for Positive Integer:
1.6094379124341003
The Output for Positive float:
3.5263605246161616
上面的代码计算每种数据类型的对数,并返回双精度、整数、浮点数、无穷大和零输入值的输出值。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
相关文章
如何在 Java 中延迟几秒钟的时间
发布时间:2023/12/17 浏览次数:217 分类:Java
-
本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。
如何在 Java 中把 Hashmap 转换为 JSON 对象
发布时间:2023/12/17 浏览次数:187 分类:Java
-
它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。
如何在 Java 中按值排序 Map
发布时间:2023/12/17 浏览次数:171 分类:Java
-
本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map
进行排序,并列出了一些示例代码来理解它。
如何在 Java 中打印 HashMap
发布时间:2023/12/17 浏览次数:192 分类:Java
-
本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。
在 Java 中更新 Hashmap 的值
发布时间:2023/12/17 浏览次数:146 分类:Java
-
本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。
Java 中的 hashmap 和 map 之间的区别
发布时间:2023/12/17 浏览次数:79 分类:Java
-
本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,
在 Java 中获取用户主目录
发布时间:2023/12/17 浏览次数:218 分类:Java
-
这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。
Java 中 size 和 length 的区别
发布时间:2023/12/17 浏览次数:179 分类:Java
-
这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。
Java 中的互斥锁
发布时间:2023/12/17 浏览次数:111 分类:Java
-
了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,