0

I have made a little code. So far I just built a do while loop that works with BigDecimal. To be more precise, the counter variable of the loop is a BigDecimal. I already put much effort to it although it might not look like that (it's short).. But that's they first time I work with those numbers.

So in this case the loop will be repeated 25 times. What I wanted do next is using power. I want do 2^counter variable. How can I build this into my code?

import java.math.BigDecimal;
import java.math.*;

public class MiniDecimals {
    public static void main(String[] args){
        BigDecimal bigCount = new BigDecimal("0");
        BigDecimal bigiMax = new BigDecimal("25"); // we stop the loop when we reach 25
        BigDecimal testPow = new BigDecimal("2");
        int res;
        MathContext mc = new MathContext(10);

        do{
            testPow = testPow.pow(bigCount, mc);
            System.out.println(testPow);

            bigCount = bigCount.add(new BigDecimal(1)); 
            res = bigCount.compareTo(bigiMax); 
        }while(res<0);
    }
}

I get error: The method pow(int, MathContext) in the type BigDecimal is not applicable for the arguments (BigDecimal, MathContext)

I have looked this up on the internet already but nothing has helped me so far. Is there a way to do this without changing my code entirely? This is no homework. I'm just trying to learn about big integers and doubles.

cnmesr
  • 345
  • 3
  • 11
  • This might help: https://stackoverflow.com/questions/16441769/javas-bigdecimal-powerbigdecimal-exponent-is-there-a-java-library-that-does – Azeem Jun 17 '17 at 15:55
  • Id something doesn't compile, it makes sense to first read the actual error message. This one tells you the declared arguments are `pow(int, MathContext)`and that you passed `(BigDecimal, MatchContext)`. That says it all. If still in doubt, check the actual documentation, which is often directly reachable from your IDE. – Rudy Velthuis Jun 18 '17 at 23:45

1 Answers1

2

The first argument to pow is not a BigDecimal, but rather an int.

(On a side note, you're aware that testPow will immediately reach and remain at 1, yes?)

Joe C
  • 15,324
  • 8
  • 38
  • 50
  • Oh alright.. Isn't there a simple way to do it for BigDecimal as well? And thank you I wasn't aware of it. Will change the start of loop from zero to one in my code. – cnmesr Jun 17 '17 at 16:57
  • It may be worth reading the [Javadoc](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html) for `BigDecimal` to learn exactly what you can and cannot do with it. – Joe C Jun 17 '17 at 17:10