15

I am using BigDecimal to make some calculations. Recently I ran into:

java.lang.ArithmeticException: 
Non-terminating decimal expansion; no exact representable decimal result.

The answer to that problem was posted here: ArithmeticException: "Non-terminating decimal expansion; no exact representable decimal result"

This means, there is some division with unlimited decimals, so BigDecimal tells me it cannot calculate the result exactly. To avoid this I have to call BigDecimal.setScale(SOMETHING, ROUNDING_MODE);

EDIT:

The problem now is to set the SOMETHING to the maximum possible value. I could use a MathContext with precision below UNLIMITED (MathContext(precision)) but the same problem occurrs there. There would need to be a value below MathContext.UNLIMITED...

Does anyone know how to accomplish that?

Moved my second question to: Why is there no BigDecimal.setPrecision() method?

Thank you!

Oliver

Community
  • 1
  • 1
4thfloorstudios
  • 388
  • 1
  • 6
  • 16
  • 4
    I doubt that's actually what you really want - you'd get such a huge number of decimals that it would be very weird if they were all useful. – harold Aug 07 '13 at 15:22
  • You seem to be interested in [this](http://www.strangehorizons.com/2001/20010402/biggest_numbers.shtml) – devnull Aug 07 '13 at 15:25
  • The maximum number of decimals is determined by your memory or some other large limits, like 2^32^32 figures. Working with such large numbers will cause performance issues well before you reach the limits... – assylias Aug 07 '13 at 15:28
  • Perhaps you're looking for BigRational? In any case, please explain what you need this for, so we can give a more useful answer. – harold Aug 07 '13 at 15:31
  • @harold: I want to achieve the maximum possible exactness for a division result. After all I have to scale it to 6 decimals. – 4thfloorstudios Aug 07 '13 at 15:39
  • I'm afraid I don't really understand that, it seems contradictory to me. Could you explain what this is for? The context? What are you dividing, and why, and what exactly are you doing with the result? – harold Aug 07 '13 at 15:46
  • Sorry, you won't be able to compute the number you want. You will soon run out of memory and out of disc space, with everything filled up with digits from your calculation. How will you even be able to print out the result from your calculation? – stefan.schwetschke Aug 07 '13 at 15:54

2 Answers2

4

First Question: There is no such thing as BigDecimal.UNLIMITED. That doesn't make any sense. What is one less than infinity? Because the docs don't explicitly mention the default MathContext of a BigDecimal, I have to assume that it is MathContext.UNLIMITED. Seeing as you have a repeating decimal, a BigDecimal can't hold it, even with unlimited precision. Instead, you need to arbitrarily pick a value (say 100 or 1000 - I have no idea what you are using these numbers for). If you only care about the first, say, 15 decimal places, you should never have a rounding error if you track the first, say, 100.

Second Question: Ask a separate question for it.

supersam654
  • 3,126
  • 33
  • 34
  • @supersam654: You are right about "1 less than infinity" (and the UNLIMITED constant, sorry for that...) but what does BigDecimal work with per default then? From the [docs](http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html): "The BigDecimal class gives its user complete control over rounding behavior. If no rounding mode is specified and the exact result cannot be represented, an exception is thrown;" So, where is the limit?!? I did not find any documentation about that. – 4thfloorstudios Aug 07 '13 at 15:42
  • @4thfloorstudios I couldn't find any reference to the default in the documentation. Therefore, I have to assume that it defaults to MathContext.UNLIMITED. – supersam654 Aug 07 '13 at 16:35
  • 1
    If it's a repeating decimal that means it's a rational number, so you could store it as a numerator/denominator pair, or use [a library that does that for you](http://stackoverflow.com/questions/5442640). – BlueRaja - Danny Pflughoeft Aug 07 '13 at 17:18
0

BigDecimal.setScale takes an int as its argument. So the maximum possible scale is

2,147,483,647

But as mentioned above, you should not use such huge scales. The number you are computing will eat up lots of memory without any useful purpose. Computing the number might take a very long time.

stefan.schwetschke
  • 8,862
  • 1
  • 26
  • 30