[Noob Question] Raising a number to a power

Discussion in 'Plugin Development' started by swimmer1929, Sep 28, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    swimmer1929

    Hey there. So I'm making a calculator plugin and am having a bit of trouble on raising a number to a power. Here's the current code for power part (please try to refrain from laughing):
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("calculate"))
    3. {
    4. if (args.length == 3)
    5. {
    6. if (args[1].equalsIgnoreCase("^"))
    7. {
    8.  
    9. int num = 0;
    10. int pow = 0;
    11. int answer = 0;
    12. String num1 = args[0];
    13. num = Integer.parseInt(num1);
    14. String pow1 = args[2];
    15. pow = Integer.parseInt(pow1);
    16.  
    17.  
    18.  
    19. }


    Yeah.... so as you can see I the numbers ready (the command should look like this /calculate (# here) ^ (power here)) however I have no idea what to do with them in order to raise the number (arg[0] or num) to the power (arg[2] or pow). Thanks!
     
  2. Offline

    KoolSource

    To do a calculation for exponents just do this:

    Code:
    Int i = 1;
    while(i < pow){
    i++;
    num = num*num;
    }
    answer=num;
    
    If I'm thinking about that correctly in my head, it should work properly. You may need to set i to equal 0 instead of 1, but I think the way I did it is correct.

    You may want to do some type of logic to verify that your numbers are in fact numbers before you reach the calculation step. If you do not do this you'll end up getting a Java error stating that your string can't be converted to an integer if anything other than an integer is entered. (Eg. 1.4 or "Cat"). If you want to be able to support decimals, I would suggest using Double as your declaration for the num variable. As for the exponent, in order to do decimal exponents you would likely need much more code than what I've shown you above. Let me know if you do need to have decimals in your exponents and I'll do a little more research as to how to do this.

    -John
     
  3. Offline

    Gopaintman

  4. Offline

    KoolSource

    This is obviously MUCH better than my answer :D
     
  5. Offline

    Gopaintman

    Yea the Java libraries do exist for a reason :p
     
  6. Offline

    KoolSource

    Gopaintman I'm experience with the logic and syntax of Java, but do not know about many libraries so I end up doing things like this all the time when there's a simple 1 line solution to my problem :(

    -John
     
  7. Offline

    The_Doctor_123

    lol. It was funny how KoolSource was like typin' away then Gopaintman gave a simple one line answer. :p

    But yeah, lesson learned, check Java's APIs for widely used things.
     
    KoolSource and Gopaintman like this.
  8. Offline

    swimmer1929

    Haha, thanks for the simple answer and thanks to everyone else for the help!
     
  9. Offline

    Gopaintman

    Anytime!
     
Thread Status:
Not open for further replies.

Share This Page