-1

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).

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.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Aditya Gupta
  • 633
  • 1
  • 4
  • 11

2 Answers2

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:

Returns the base 10 logarithm of a double value

Michael
  • 41,989
  • 11
  • 82
  • 128
ernest_k
  • 44,416
  • 5
  • 53
  • 99
0

Math.log10() is the standard function found in Math library:

public static double log10(double a)

Returns the base 10 logarithm of a double value. Special cases:

• If the argument is NaN or less than zero, then the result is 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.

The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Community
  • 1
  • 1
bipll
  • 11,747
  • 1
  • 18
  • 32