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

BigDecimal to the power of BigDecimal on Java/Android

Posted by: admin June 15, 2020 Leave a comment

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.

How to&Answers:

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 );

Answer:

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. 😉

Answer:

Look into “Java Number Cruncher: The Java Programmer’s Guide to Numerical Computing” – Chapter 12.5 Big Decimal Functions : there is some source code with as exact as possible algorithm to compute exponential and logarithm.

Using the mathematical formula : x^y=exp(y*ln(x)) you can achieve a result with maximum precision.

Nevertheless doubles do have a good precision and I strongly recommand you test if it’s not precise enough for your need.

Answer:

There is a good library ApFloat: http://www.apfloat.org/