2

i am trying to find the power of a value.But the problem is my exponent is a fractional value.power function does not suppporting any datatype other than int.

 BigDecimal fd_returns_at_time_of_replace=(BigDecimal.valueOf(capitalDiff).multiply((BigDecimal.valueOf((long)constant1+.09)).pow(temp)));

here temp is a fractional value.given below is the eror message i am getting.

The method pow(int) in the type BigDecimal is not applicable for the arguments (double)

please anybody help me to do this.

andro-girl
  • 7,989
  • 22
  • 71
  • 94

2 Answers2

1

BigDecimal.pow() only takes an int. To see a cool example of writing BigDecimal.pow() that accepts a double, see this question How to do a fractional power on BigDecimal in Java?

Community
  • 1
  • 1
spatulamania
  • 6,613
  • 2
  • 30
  • 26
  • i read that answer.but i could not understand anything.i am not that familiar with android.can you please simplfy that. – andro-girl Oct 20 '11 at 06:43
1

Common Sense

Consider you want to raise the number x to the power y

If both are integers:

for(int i=0 ; i<y ; i++)
    answer = answer * x;

Problems are only when y is a decimal!

So we first change y to the form of y = n + 1/d

How to do that:

  • n = floor of y
  • d = 1 / (y - n) << integer

Now x^y = x^n * x^1/d

  • x to the power n is simple using the usual method
  • x to the power 1/d is simply the d th root of x

Note: You can increase the precision of your function by reducing the error factor induced by makind d an integer. How! 1/d can be multiplied by powers of 10.

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106