java 如何在java中计算对数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25973731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate logarithms in java?
提问by Muhammad Haryadi Futra
I'm trying to write a Java program that can take values and put them into a formula involving log 1/3.
我正在尝试编写一个 Java 程序,该程序可以取值并将它们放入一个涉及 log 1/3 的公式中。
How can I calculate log 1/3 in Java?
如何在 Java 中计算 log 1/3?
回答by CoderCroc
You can use Math.log(value)
to get log of specific value where value is considered to be in double
.You can also use Math.log10(value)
to get base 10 log.
您可以使用Math.log(value)
获取特定值的日志,其中值被认为是在double
.You 也可以使用Math.log10(value)
获取基数为 10 的日志。
So you can just use
所以你可以使用
Math.log(1/3.0)
回答by Stefan Dollase
When you want to calculate the logarithm you need to know the base. Once you know the base you can perform the calculation:
当你想计算对数时,你需要知道底数。一旦你知道了基数,你就可以进行计算:
log_b(x) = ln(x) / ln(b)
http://en.wikipedia.org/wiki/Logarithm#Change_of_base
http://en.wikipedia.org/wiki/Logarithm#Change_of_base
In Java the Math#log(double)function calculates the natural logarithm. So you can use this to calculate the logarithm to a given base b:
在 Java 中,Math#log(double)函数计算自然对数。所以你可以用它来计算给定底 b 的对数:
double result = Math.log(x) / Math.log(b);
回答by Puspam
1/3 yields the result as integer type value. So, the result will be 0, not 0.3333.
1/3 将结果作为整数类型值。因此,结果将是 0,而不是 0.3333。
Therefore, Math.log(1/3) = Math.log(0), which results in infinity.
因此,Math.log(1/3) = Math.log(0),结果为无穷大。
So, you need to write Math.log(1/3.0) to get the desired result.
因此,您需要编写 Math.log(1/3.0) 以获得所需的结果。
回答by MrTux
You can calculate the natural logarithm using the Java.lang.Math.log() method:
您可以使用 Java.lang.Math.log() 方法计算自然对数:
System.out.println("Math.log(1/3.0)=" + Math.log(1/3.0));
See http://www.tutorialspoint.com/java/lang/math_log.htmand http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#log%28double%29
请参阅http://www.tutorialspoint.com/java/lang/math_log.htm和http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#log%28double%29
In order to get the log_10 you can do it as follows:
为了获得 log_10,您可以按如下方式进行:
System.out.println("log_10(1/3.0)=" + (Math.log(1/3.0)/Math.log(10)));
回答by Ruchira Gayan Ranaweera
You don't need a new implementation there is inbuilt function for this
你不需要一个新的实现有内置的功能
Read about Math.log()
But in this case log(1/3)
will give you value as infinity
,If you use Math.log(1/3)
.
但在这种情况下log(1/3)
会给你的价值infinity
,如果你使用Math.log(1/3)
.
You can use log
rule as follows.
您可以log
按如下方式使用规则。
log(1/3) =log(1)-log(3)
Now
现在
Math.log(1/3)=Math.log(1)-Math.log(3)
Eg:
例如:
System.out.println(Math.log(1)-Math.log(3));
Out put:
输出:
-1.0986122886681098