0

I am trying to find the result of log(10^k) , where k is big number like 10000. For example :

BigDecimal first = BigDecimal.TEN.pow(10000);
double result = Math.log(first.doubleValue());

However "result" becomes Infinity , however on wolphram approximates it to 23025.85.Any suggestion how to find the result? As a result the number with the first two digits after the decimal point are enough for me.

Mark
  • 31
  • 1
  • 6
  • What do you think the value of `first.doubleValue()` is? – Andy Turner Jun 16 '20 at 15:46
  • 5
    `log(10^k) = k*log(10)`. – Andy Turner Jun 16 '20 at 15:47
  • Infinity? also it is natural logarithm not log10 – Mark Jun 16 '20 at 15:47
  • @TharinduSathischandra no, but `first.doubleValue()` is, because it is bigger than `Double.MAX_VALUE`. – Andy Turner Jun 16 '20 at 15:50
  • 1
    Did you mean the natural log? I ask because log10(10^k) == k – WJS Jun 16 '20 at 15:50
  • Have a look at https://stackoverflow.com/questions/739532/logarithm-of-a-bigdecimal – Tharindu Sathischandra Jun 16 '20 at 15:53
  • I wonder why the writers of the `Math` class didn't just use `log` for base 10 and `ln` for the natural log as one might expect? Imho, it could avoid confusion. – WJS Jun 16 '20 at 15:56
  • Around here it would be reasonable to assume log base 2. – pjs Jun 16 '20 at 15:56
  • @WJS Because in calculus it's very common to write *log* for the natural logarithm. But I'm on your side, I think *ln* would be just nicer. – akuzminykh Jun 16 '20 at 16:00
  • @akuzminykh Actually, all my calculus books used `ln` – WJS Jun 16 '20 at 16:01
  • @WJS Introductory calculus books (particularly ones intended for high school) may use `ln` because that is what students are familiar with, but as you go farther on, it is universally called `log`. The first programming languages (eg FORTRAN) were written by people with advanced math backgrounds, so they all called it `log`. That naming stuck. – btilly Jun 16 '20 at 17:49
  • 2 years of engineering calculus and advanced engineering calculus in college and it was `ln` Although both are used, See [Natural Logarithm](https://en.wikipedia.org/wiki/Natural_logarithm). But it was not my intent to start a debate. – WJS Jun 16 '20 at 17:59

3 Answers3

8

Use the fact that

log(10^k) = k*log(10)

So:

System.out.println(10000 * Math.log(10));

Prints:

23025.850929940458
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

The problem you are likely having, is that Wolphram is able to either hold the powered value or it is doing the log operation first.

When running this like your example, you will have an extremely large number that goes past the maximum value for a BigDecimal, which should result in an error or an "infinity", because it overflows the capability of the data type, I would suggest doing the operation the other way arround, perhaphs process the log first on a base 1 value for example and only then multiply it by whatever powered number you are tying to use.

0

See, there is a simple property of logarithms that you can use:

log(x^y) = y*log(x)

So what you can do is:

double y = y*log(x); 
System.out.println(Math.round(y));

Hope this helps!