Computing an appropriate division scale for pow with negative arguments?

privatestatic BigDecimal pow(BigDecimal bd,int n){

return n < 0 ? BigDecimal.ONE.divide(bd.pow(-n), _ARBITRARY_NUMBER_ , BigDecimal.ROUND_HALF_UP) : bd.pow(n);

}

Rather than plugging in an arbitrarily large number for the scale in the division operation, I would like to try to set the value appropriately. I tried simple formula that basically mulitiplied bd * n / 10, which worked nicely for some values, but gave overly large values for others.

Based on the data that I expect to process I think I could set it to 3000, but I would prefer not to take the risk, and it's just plain stupid to make all calls to the function pay the penalty.

Is there a algorithm that could approximate the value of what the final answer will be so I can plug an appropriate value into the scale?

[1009 byte] By [billrobertsona] at [2007-10-2 2:29:57]
# 1
I have made a note of this thread and will return after the [url http://www.toothycat.net/~pjt33/strike.txt]forum strike[/url].
YAT_Archivista at 2007-7-15 20:21:42 > top of Java-index,Other Topics,Algorithms...
# 2

> Is there a algorithm that could approximate the value of what the final answer will be so I can

> plug an appropriate value into the scale?

You don't need a scale for the pow operation, so first do BigDecimal recip = bd.pow(-n)

Now the final answer is 1 / recip, so the scale is roughly -log10(recip). Google BigDecimal logarithm and you'll find a few open source libraries which can do that stage. (In fact IIRC you'll find a library which does arbitrary powers of BigDecimals).

YAT_Archivista at 2007-7-15 20:21:42 > top of Java-index,Other Topics,Algorithms...
# 3
Thanks for the tip. I'm targeting a 1.4 jvm so I have to write the pow algorithm. I just put the example up in the java 5 flavor for clarity.Thanks for the suggestion on google keywords to look for. I wasn't coming up with anything.
billrobertsona at 2007-7-15 20:21:42 > top of Java-index,Other Topics,Algorithms...