Results 1 to 18 of 18
Thread: BIG decimals
 09292010, 06:33 PM #1Member
 Join Date
 Sep 2010
 Posts
 31
 Rep Power
 0
BIG decimals
Hey,
I'm working on a program that will calculate pi, and I need a way to store digits beyond the 16 that a double can hold. I've looked into BigDecimals, but can't find out how many digits of a number that they can hold. Does anyone know how many digits bigdecimals hold or another way to store many digits usable in math (so not a string)?
Thanks
 09292010, 06:46 PM #2Member
 Join Date
 Sep 2010
 Posts
 31
 Rep Power
 0
I meant, is there something more precise than a LONG? BigDecimal question still standing.
 09292010, 07:02 PM #3
BigDecimal is basically an integer without a maximum size that is scaled, using 10^(scale). Therefore, because it is arbitrary precision, it does not have a maximum size until the computer runs out of memory.
 09292010, 07:37 PM #4Member
 Join Date
 Sep 2010
 Posts
 31
 Rep Power
 0
So... is it as accurate as an int (not to very many places)?
 09292010, 08:33 PM #5
An Integer is a wrapper for the primitive type, int.
 Range: 2^31 to 2^31  1
A BigInteger is an arbitraryprecision type of integer, capable of storying any integer of any size, so long as the computer has enough memory to store it.
 Range: infinity to +infinity (no decimal precision)
A BigDecimal is essentially a BigInteger multiplied by 10^(x) where x is some scale (0 to infinity, as far as I'm aware), which means that it can be infinitely precise if your computer is powerful enough.
 Range: 0.999999999999999 to +0.999999999999999 (both repeated to infinity)
 09292010, 11:21 PM #6Member
 Join Date
 Sep 2010
 Posts
 31
 Rep Power
 0
Fantastic! The arbitrary precision aspect is awesome... in concept. Whenever I go to divide two BigDecimals though, the result is either a total loss of precision in rounding or an error. Say I wanted to divide a big decimal equal to two by another equal to three, I could round and have a result of one, or I'd get an error saying "Exception in thread "main" java.lang.ArithmeticException: Rounding necessary". I understand what that means... but is there a way around this?? I tried multiplying the numerator up by a constant then performing the division with rounding, then dividing the quotient by the above constant. Like so:
Java Code://numer=numerator, digitcount= "constant", denom= denominator, //local = quotient numer=numer.multiply(digitcount); BigDecimal local = numer.divide(denom, RoundingMode.CEILING); local=local.divide(digitcount, RoundingMode.UNNECESSARY); numer=numer.divide(digitcount, RoundingMode.UNNECESSARY);
 09302010, 12:29 AM #7
This is because BigDecimal cannot handle nonterminating sequences, which is a bit unfortunate. However, there is a bit of a work around:
Create a function that "tries" to divide using BigDecimal; if it fails, "catch" the error and return a double division instead. Something like this:
Java Code:{ BigDecimal two = new BigDecimal(2); BigDecimal three = new BigDecimal(3); System.out.println(base.DivideDecimals(two,three).toString()); System.out.println(base.DivideDecimals(three,two).toString()); } public static BigDecimal DivideDecimals(BigDecimal a, BigDecimal b) { try { System.out.println("Attempting BigDecimal division.."); return a.divide(b); // Don't attempt to round here. } catch (ArithmeticException ae) { System.out.println("BigDecimal division failed. Using doubles."); return new BigDecimal(a.doubleValue() / b.doubleValue()); } }
Java Code:run: Attempting BigDecimal division.. BigDecimal division failed. Using doubles. 0.66666666666666662965923251249478198587894439697265625 Attempting BigDecimal division.. 1.5 BUILD SUCCESSFUL (total time: 1 second)
Much luck!
 09302010, 01:16 AM #8Member
 Join Date
 Sep 2010
 Posts
 31
 Rep Power
 0
Hey sick it worked! Only one more thing... is there a way to limit the number of digits used? Although I want a lot of digits, I don't want so many that it's too much for the the output pane so it tells me to put it in wrap mode. I've tried multiplying by X then dividing by X with rounding... no difference. Ideas?
 09302010, 02:53 AM #9Member
 Join Date
 Sep 2010
 Posts
 31
 Rep Power
 0
Forget the last one... found the beauty of MathContexts... thanks for all your help, Zack
 09302010, 10:24 AM #10Moderator
 Join Date
 Apr 2009
 Posts
 13,532
 Rep Power
 24
Dividing doubles as a replacement for BigDecimal isn't a good idea. Presumably your are using BigDecimal (in part) to avoid the inevitable imprecision of a floatbased number. Dividing doubles will simply mean reintroducing that imprecision.
What you want to do is provide a scale to the BigDecimal, which tells it how many decimal places to use. divide() has a scale parameter. So this:
Java Code:BigDecimal bd1 = new BigDecimal(2); BigDecimal bd2 = new BigDecimal(3); BigDecimal result = bd1.divide(bd2); System.out.println(result);
Java Code:BigDecimal bd1 = new BigDecimal(2); BigDecimal bd2 = new BigDecimal(3); BigDecimal result = bd1.divide(bd2, 20, RoundingMode.HALF_UP); System.out.println(result);
0.66666666666666666667
(20 decimal places)
 09302010, 06:48 PM #11
The double is simply an alternative because most numbers dividing into nonterminating sequences can easily be scaled to doubles. Both methods work.
 09302010, 09:13 PM #12Moderator
 Join Date
 Apr 2009
 Posts
 13,532
 Rep Power
 24
Except if you want something accurate (which you will do if you are trying to calculate pi to some number of decimal places) a floatbased thing won't do, due to the inevitable innacuracies of a float.
It's why currency is always held as a BigDecimal.
 09302010, 09:50 PM #13
Since when is currency always held as a BigDecimal? That doesn't make intuitive sense. Currency is only ever held to two decimal places, with the third as a reference ($0.00 ± $0.001). As you saw in my output above, the double was accurate to 17 decimal places + a rounding decimal place. If you carried five decimal places, that would be excessively accurate for currency.
 10012010, 09:42 AM #14Moderator
 Join Date
 Apr 2009
 Posts
 13,532
 Rep Power
 24
In any financial system I have worked on, that's where.
Currency conversion, for example is usually held to 5 decimal places, and you'll start to lose more than pennies if that isn't accurate...
 10022010, 01:21 AM #15
I just converted an arbitrary value of the highestvalue currency (Kuwait Dinar) to the lowestvalue currency (Zimbabwe Dollar), and didn't lose any precision by trimming to 5 decimal places.
Java Code:12,345.67891 KWD = 15,724,848.09 ZWD 12,345.67891234568015 KWD = 15,724,848.09015080519021 ZWD
 10032010, 05:43 PM #16Moderator
 Join Date
 Apr 2009
 Posts
 13,532
 Rep Power
 24
Except you have introduced a problem as soon as you are dealing in such imprecision.
Having calculations that result in 1.99999999 instead of 2.0 (just an example) means you have introduced an incorrect value into your financial data. And since these calculations usually involve large numbers of numbers, each with their own potential innacuracy, you will (yes will, I have seen it) get results that are wrong. And these results leave you liable when the tax man comes to visit (or a client queries a total).
There is a reason that Sun, Oracle, IBM (to name but three) point out that all financial data should be handled using BigDecimals, or longs, rather than float/double.
 10042010, 04:40 AM #17
You're still truncating the data to 20 decimal places though in the example above, whilst mine basically truncates it to 16ish. I know that's a factor of 10^4 but is it likely that you're going to be adding so many numbers that 1*10^17 is important? Likely not.
 10042010, 11:14 AM #18Moderator
 Join Date
 Apr 2009
 Posts
 13,532
 Rep Power
 24
Similar Threads

Formatting numbers or decimals (around comma)
By Joris in forum Advanced JavaReplies: 1Last Post: 04222010, 06:46 PM 
Decimals in java help!!
By Gold in forum New To JavaReplies: 3Last Post: 12102009, 12:25 AM 
number to word with decimals
By fertas in forum New To JavaReplies: 3Last Post: 08302008, 04:12 AM 
Java Program To Convert A Number In To Words With Decimals
By javanewbie in forum New To JavaReplies: 1Last Post: 07022008, 02:58 PM 
get more decimals?!?! please help!
By michcio in forum New To JavaReplies: 7Last Post: 05222008, 11:26 PM
Bookmarks