Home » Android » BigDecimal to the power of BigDecimal on Java/Android

# BigDecimal to the power of BigDecimal on Java/Android

Questions:

I have a simple BigDecimal that I want to power to another BigDecimal, for example 11.11^-1.54. What would be the neatest way to do it in Java/Android? I don’t like the idea of converting to doubles because it is for a medical application, so I would appreciate the biggest presicion possible. So far I have reviewed http://commons.apache.org/math/ and math stuff from Google Guava, but found nothing.

Edit:
The whole calculation is complex, and has many operations like this. I need as much precision at the end as possible.

A utility class for BigDecimal https://github.com/tareknaj/BigFunctions

Example for z = x^y –> z = exp ( ln(x) * y )

``````final int SCALE = 10;
BigDecimal x = new BigDecimal(1);
BigDecimal y = new BigDecimal(12);

BigDecimal z = BigFunctions.exp( BigFunctions.ln(x, SCALE).multiply(y),SCALE );
``````

How many digits of precision do you need? You are only using 4 digits in your example. If this is medical and there for real world, you can only measure most things in the real world to 10-13 digits of accuracy and double has up to 16 digits of accuracy.

``````System.out.println(Math.pow(11.11, -1.54));
``````

prints

``````0.024524510581710988
``````

If you use a library from this book http://www.apropos-logic.com/nc/, you can get

``````int runs = 10000;

long start = System.nanoTime();
double x1 = 0;
for (int i = 0; i < runs; i++)
x1 = Math.pow(11.11, -1.54);
long time = System.nanoTime() - start;
System.out.println(x1 + " took " + time / runs / 1e3 + " us avg.");

long start2 = System.nanoTime();
BigDecimal x2 = null;
for (int i = 0; i < runs; i++)
x2 = exp(ln(BigDecimal.valueOf(11.11), 20).multiply(BigDecimal.valueOf(-1.54)), 20);
long time2 = System.nanoTime() - start2;
System.out.println(x2 + " took " + time2 / runs / 1e3 + " us avg.");
``````

prints (us for micro-seconds)

``````0.024524510581710988 took 0.478 us avg.
0.02452451058171098739 took 603.769 us avg.
``````

with 40 digits of precision

``````0.0245245105817109873886495555036930857940 took 1409 us avg.
``````

which may still be fast enough on your device.

I haven’t included the code, partly because its very long. I am impressed how fast it is. 😉