438,779 Members | 1,078 Online Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

# Power function in Java

 P: 2 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 Dec 11 '06 #1
11 Replies

 10K+ P: 13,264 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)? Dec 11 '06 #2

 Expert 2.5K+ P: 3,652 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. Dec 11 '06 #3

 10K+ P: 13,264 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. Expand|Select|Wrap|Line Numbers   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 Dec 11 '06 #4

 Expert 2.5K+ P: 3,652 >.> fine. Dec 11 '06 #5

 10K+ P: 13,264 >.> fine. What do you mean fine? Dec 11 '06 #6

 Expert 2.5K+ P: 3,652 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. Dec 11 '06 #7

 10K+ P: 13,264 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. Dec 11 '06 #8

 Expert 2.5K+ P: 3,652 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: Expand|Select|Wrap|Line Numbers 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.something, but I can't figure out how to change the version of Java BlueJ uses. Dec 11 '06 #9

 10K+ P: 13,264 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: Expand|Select|Wrap|Line Numbers 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.something, 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 Dec 11 '06 #10

 Expert 2.5K+ P: 3,652 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 Dec 11 '06 #11

 10K+ P: 13,264 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 Dec 12 '06 #12 