Power function in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anibio06
    New Member
    • Dec 2006
    • 2

    #1

    Power function in Java

    hi guys,

    i want to find 2^n (pow(2,n)).

    I am doing java.lang.Math and using power function. But error occured. Because my variable is integer type. but the function arguments are double.

    Can u give me a solution to find 2^n using java
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by anibio06
    hi guys,

    i want to find 2^n (pow(2,n)).

    I am doing java.lang.Math and using power function. But error occured. Because my variable is integer type. but the function arguments are double.

    Can u give me a solution to find 2^n using java
    Why not change your argument to double then with pow(2.0, n)?

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Exactly what I was going to suggest. Math.pow() either takes two integers or two doubles, but not a mix of the two, for some reason.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Ganon11
        Exactly what I was going to suggest. Math.pow() either takes two integers or two doubles, but not a mix of the two, for some reason.
        Code:
         
        public class Power {
         public static void main(String[] args) {
          int y = 2;
          double x = Math.pow(2.1, 7);
          x = Math.pow(y, 7);
          x = Math.pow(y, 7.7);
          System.out.println(x);
         }
        }
        This compiles on 1.5

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          >.>

          fine.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Ganon11
            >.>

            fine.
            What do you mean fine?

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Well, I was just being slightly sarcastic. Whenever I had used Java, it didn't let me use an int and a double - I was getting the same error as anibio, and that's why I replied the way I did. Then you basically said I was wrong according to Java 1.5. So I was 'fake' frustrated - no big deal though.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by Ganon11
                Well, I was just being slightly sarcastic. Whenever I had used Java, it didn't let me use an int and a double - I was getting the same error as anibio, and that's why I replied the way I did. Then you basically said I was wrong according to Java 1.5. So I was 'fake' frustrated - no big deal though.
                Actually I'm not fully satisfied yet. I haven't tried it on a 1.4 compiler yet so I'm not sure about the behaviour on 1.4 yet since 1.5 has this autoboxing thing added to it which may have helped. The docs have Math.pow(double x, double y) both for 1.4 and 1.5 so I don't think the behaviour will be different.

                I was afraid you'd got too frustrated with my reply.

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  Hmm...using java1.5.0_04 (through BlueJ) I get the results:
                  9.0
                  8.0
                  8.0
                  8.0
                  8.0
                  4.0
                  4.0
                  4.0
                  4.0
                  8.0
                  9.0
                  8.0
                  9.0
                  8.0
                  9.0
                  8.0
                  9.0
                  8.0
                  8.0
                  8.0
                  8.0

                  from the program:

                  Code:
                  public class Tester {
                      public static void main(String[] args) {
                          int n = 2;
                          double x = 2.0;
                          System.out.println(Math.pow(n, x)); // Variable tests...
                          System.out.println(Math.pow(x, n));
                          System.out.println(Math.pow(x, x));
                          System.out.println(Math.pow(n, n));
                          
                          System.out.println(Math.pow(n, 3.0)); // Mixed tests (n) ...
                          System.out.println(Math.pow(3.0, n));
                          System.out.println(Math.pow(n, 3));
                          System.out.println(Math.pow(3, n));
                          
                          System.out.println(Math.pow(x, 3.0)); // Mixed tests (x) ...
                          System.out.println(Math.pow(3.0, x));
                          System.out.println(Math.pow(x, 3));
                          System.out.println(Math.pow(3, x));
                          
                          System.out.println(Math.pow(2, 3)); // Raw Number tests
                          System.out.println(Math.pow(2.0, 3));
                          System.out.println(Math.pow(2, 3.0));
                          System.out.println(Math.pow(2.0, 3.0));
                      }
                  }
                  This computer has java1.4.somethi ng, but I can't figure out how to change the version of Java BlueJ uses.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Ganon11
                    Hmm...using java1.5.0_04 (through BlueJ) I get the results:
                    9.0
                    8.0
                    8.0
                    8.0
                    8.0
                    4.0
                    4.0
                    4.0
                    4.0
                    8.0
                    9.0
                    8.0
                    9.0
                    8.0
                    9.0
                    8.0
                    9.0
                    8.0
                    8.0
                    8.0
                    8.0

                    from the program:

                    Code:
                    public class Tester {
                    public static void main(String[] args) {
                    int n = 2;
                    double x = 2.0;
                    System.out.println(Math.pow(n, x)); // Variable tests...
                    System.out.println(Math.pow(x, n));
                    System.out.println(Math.pow(x, x));
                    System.out.println(Math.pow(n, n));
                     
                    System.out.println(Math.pow(n, 3.0)); // Mixed tests (n) ...
                    System.out.println(Math.pow(3.0, n));
                    System.out.println(Math.pow(n, 3));
                    System.out.println(Math.pow(3, n));
                     
                    System.out.println(Math.pow(x, 3.0)); // Mixed tests (x) ...
                    System.out.println(Math.pow(3.0, x));
                    System.out.println(Math.pow(x, 3));
                    System.out.println(Math.pow(3, x));
                     
                    System.out.println(Math.pow(2, 3)); // Raw Number tests
                    System.out.println(Math.pow(2.0, 3));
                    System.out.println(Math.pow(2, 3.0));
                    System.out.println(Math.pow(2.0, 3.0));
                    }
                    }
                    This computer has java1.4.somethi ng, but I can't figure out how to change the version of Java BlueJ uses.

                    I got

                    4.0
                    4.0
                    4.0
                    4.0
                    8.0
                    9.0
                    8.0
                    9.0
                    8.0
                    9.0
                    8.0
                    9.0
                    8.0
                    8.0
                    8.0
                    8.0

                    On 1.5_08

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      Whoops, on that last post you can ignore the first 5 values. They somehow got copied onto my paste clipboard when copying from the results.

                      I got these results at home using java1.4.1_07
                      4.0
                      4.0
                      4.0
                      4.0
                      8.0
                      9.0
                      8.0
                      9.0
                      8.0
                      9.0
                      8.0
                      9.0
                      8.0
                      8.0
                      8.0
                      8.0

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by Ganon11
                        Whoops, on that last post you can ignore the first 5 values. They somehow got copied onto my paste clipboard when copying from the results.

                        I got these results at home using java1.4.1_07
                        4.0
                        4.0
                        4.0
                        4.0
                        8.0
                        9.0
                        8.0
                        9.0
                        8.0
                        9.0
                        8.0
                        9.0
                        8.0
                        8.0
                        8.0
                        8.0
                        Therefore no surprises as expected. I wonder what problem the OP was getting on this one

                        Comment

                        Working...