By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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

Ganon11
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
  1.  
  2. public class Power {
  3.  public static void main(String[] args) {
  4.   int y = 2;
  5.   double x = Math.pow(2.1, 7);
  6.   x = Math.pow(y, 7);
  7.   x = Math.pow(y, 7.7);
  8.   System.out.println(x);
  9.  }
  10. }
  11.  
This compiles on 1.5
Dec 11 '06 #4

Ganon11
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

Ganon11
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

Ganon11
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
  1. public class Tester {
  2.     public static void main(String[] args) {
  3.         int n = 2;
  4.         double x = 2.0;
  5.         System.out.println(Math.pow(n, x)); // Variable tests...
  6.         System.out.println(Math.pow(x, n));
  7.         System.out.println(Math.pow(x, x));
  8.         System.out.println(Math.pow(n, n));
  9.  
  10.         System.out.println(Math.pow(n, 3.0)); // Mixed tests (n) ...
  11.         System.out.println(Math.pow(3.0, n));
  12.         System.out.println(Math.pow(n, 3));
  13.         System.out.println(Math.pow(3, n));
  14.  
  15.         System.out.println(Math.pow(x, 3.0)); // Mixed tests (x) ...
  16.         System.out.println(Math.pow(3.0, x));
  17.         System.out.println(Math.pow(x, 3));
  18.         System.out.println(Math.pow(3, x));
  19.  
  20.         System.out.println(Math.pow(2, 3)); // Raw Number tests
  21.         System.out.println(Math.pow(2.0, 3));
  22.         System.out.println(Math.pow(2, 3.0));
  23.         System.out.println(Math.pow(2.0, 3.0));
  24.     }
  25. }
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
  1. public class Tester {
  2. public static void main(String[] args) {
  3. int n = 2;
  4. double x = 2.0;
  5. System.out.println(Math.pow(n, x)); // Variable tests...
  6. System.out.println(Math.pow(x, n));
  7. System.out.println(Math.pow(x, x));
  8. System.out.println(Math.pow(n, n));
  9.  
  10. System.out.println(Math.pow(n, 3.0)); // Mixed tests (n) ...
  11. System.out.println(Math.pow(3.0, n));
  12. System.out.println(Math.pow(n, 3));
  13. System.out.println(Math.pow(3, n));
  14.  
  15. System.out.println(Math.pow(x, 3.0)); // Mixed tests (x) ...
  16. System.out.println(Math.pow(3.0, x));
  17. System.out.println(Math.pow(x, 3));
  18. System.out.println(Math.pow(3, x));
  19.  
  20. System.out.println(Math.pow(2, 3)); // Raw Number tests
  21. System.out.println(Math.pow(2.0, 3));
  22. System.out.println(Math.pow(2, 3.0));
  23. System.out.println(Math.pow(2.0, 3.0));
  24. }
  25. }
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

Ganon11
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

Post your reply

Sign in to post your reply or Sign up for a free account.