0

I'm trying to compute a large logarithim in Java and cannot work out how to work around the overflow problem. Research says I need to use the BigDecimal class, but I cannot cast a double into this. Can anyone help?

Cheers, here's an example of what I'm trying to compute: Math.log10(13168375/4224127912)

  • 2
    I'm not sure what the issue is here. Why can't you just do `log10(13168375) - log10(4224127912)`? Where is the overflow? – Oliver Charlesworth Aug 13 '14 at 18:10
  • Why can't you just create a new `BigDecimal` from the `double` instead of casting? `BigDecimal someNumber = new BigDecimal(doublVal);` – Mike B Aug 13 '14 at 18:10
  • If you want to calculate the `log` of a BigDecimal, see [this answer](http://stackoverflow.com/a/745987/391161). – merlin2011 Aug 13 '14 at 18:11

1 Answers1

1

You have to interpret the error messages.

Y.java:16: error: integer number too large: 4224127912
        System.out.println(          Math.log10(13168375/4224127912) );
                                                     ^

This means that 4224127912 (!) is too big for an integer.

You may:

 Math.log10(13168375/4224127912.0)   // use a double literal

 Math.log10((double)13168375/4224127912L)    // use a long literal  
 Math.log10(13168375.0/4224127912L)          // use a long literal

No overflow!

And no logarithm around here is "large".

laune
  • 31,114
  • 3
  • 29
  • 42
  • @GriffeyDog Hey,thanks - I was concentrating on avoiding the error message. I had a ".0" added to the first number as well. – laune Aug 13 '14 at 18:22