
As a test for overall performance of BigDecimal, I took a short piece of code for calculating the number 'e' by serial expansion from a posting by Michael Quinlan. I am not convinced this code is correct (or at least carefull enough) in matters of convergence, but I checked the 3 classes to return exactly the same result up to 2000 digits, so for performance measurement this algorithm will do.
/**Calculates the number 'e' with the requested precision
* @param iPrecision the requested precision
* @param bdOne BigDecimal '1', used for differentating this method's signatures
* when comparing different BigDecimal implementations
*/
static String numberE(int iPrecision, be.arci.math.BigDecimal bdOne)
{
be.arci.math.BigDecimal curfact = bdOne;
be.arci.math.BigDecimal factmul = bdOne;
be.arci.math.BigDecimal curval = be.arci.math.BigDecimal.ZERO;
String sE = "";
for (int iIteration = 0; true; iIteration++)
{
//System.out.print("Iteration " + iIteration + "\r");
// divide 1 by the current factorial
be.arci.math.BigDecimal bdTerm = bdOne.divide(curfact,
iPrecision + 1,
be.arci.math.BigDecimal.ROUND_HALF_EVEN);
// add the result to the accumulated value
curval = curval.add(bdTerm);
// check convergence of the current value
String s = curval.toString().substring(0, iPrecision + 2);
if (s.equals(sE))
{
//System.out.println("");
//System.out.println(s);
break;
}
sE = s;
// move to the next factorial value
curfact = curfact.multiply(factmul);
factmul = factmul.add(bdOne);
}
return sE;
}