This question already has an answer here:
这个问题在这里已有答案:
- Using log base 10 in a formula in Java 5 answers
在Java 5答案的公式中使用日志库10
Is there a method in the Java library that I can use to find the exponent of a number? Example: If I enter 100, then I expect a return value of 2 (since 102 = 100). If I enter 10000, then I expect a return value of 4 (since 104 = 10000).
在Java库中是否有一个方法可以用来查找数字的指数?示例:如果我输入100,那么我希望返回值为2(因为102 = 100)。如果我输入10000,那么我期望返回值为4(因为104 = 10000)。
Or do I have to keep a counter and keep computing 10counter and check if that is my number. If yes return the counter else increment the counter by 1 and repeat.
或者我必须保留一个计数器并继续计算10counter并检查这是否是我的号码。如果是,则返回计数器,否则将计数器增加1并重复。
2 个解决方案
#1
1
What you're looking for is the logarithm:
你要找的是对数:
Math.log10(100); //yields 2.
Definition from google:
谷歌的定义:
Logarithm: a quantity representing the power to which a fixed number (the base) must be raised to produce a given number.
对数:表示必须提高固定数字(基数)以产生给定数字的功率的数量。
And the doc comments of Math.log10
state:
Math.log10的文档注释表示:
Returns the base 10 logarithm of a double value
返回double值的基数10对数
#2
0
Math.log10() is the standard function found in Math library:
Math.log10()是Math库中的标准函数:
public static double log10(double a)
public static double log10(double a)
Returns the base 10 logarithm of a double value. Special cases:
返回double值的基数10对数。特别案例:
• If the argument is NaN or less than zero, then the result is NaN.
•如果参数为NaN或小于零,则结果为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 negative infinity.
•如果参数为正零或负零,则结果为负无穷大。
• If the argument is equal to 10n for integer n, then the result is n.
•如果整数n的参数等于10n,则结果为n。
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.
计算结果必须在精确结果的1 ulp范围内。结果必须是半单调的。
#1
1
What you're looking for is the logarithm:
你要找的是对数:
Math.log10(100); //yields 2.
Definition from google:
谷歌的定义:
Logarithm: a quantity representing the power to which a fixed number (the base) must be raised to produce a given number.
对数:表示必须提高固定数字(基数)以产生给定数字的功率的数量。
And the doc comments of Math.log10
state:
Math.log10的文档注释表示:
Returns the base 10 logarithm of a double value
返回double值的基数10对数
#2
0
Math.log10() is the standard function found in Math library:
Math.log10()是Math库中的标准函数:
public static double log10(double a)
public static double log10(double a)
Returns the base 10 logarithm of a double value. Special cases:
返回double值的基数10对数。特别案例:
• If the argument is NaN or less than zero, then the result is NaN.
•如果参数为NaN或小于零,则结果为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 negative infinity.
•如果参数为正零或负零,则结果为负无穷大。
• If the argument is equal to 10n for integer n, then the result is n.
•如果整数n的参数等于10n,则结果为n。
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.
计算结果必须在精确结果的1 ulp范围内。结果必须是半单调的。