-1

I am trying to calculate the power as below but it is giving me 'bad operands type for binary operator '^'. I am guessing that it is a precedence issue but it still doesn't fix with inserting additional brackets

double pw = ((N - (df + 1))^2); 
k_2
  • 13
  • 1
  • 3
  • 3
    The `^` operator is not a power operator in java (it is bitwise xor). You need to use `java.lang.Math.pow(x, 2)` instead. – Chris Taylor Nov 26 '14 at 17:10
  • `^` does not signify exponentiation in java. [Here is a link to java operators table](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html). – takendarkk Nov 26 '14 at 17:10
  • @Takendarkk, sure, I'd written the comment before I recall bitwise operator – bsiamionau Nov 26 '14 at 17:11

2 Answers2

3

You should use java.lang.Math.pow(x,y)

Example: java.lang.Math.pow(2,3) returns 8

Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
Charlie
  • 978
  • 1
  • 7
  • 27
0

See this http://www.tutorialspoint.com/java/lang/math_pow.htm

java.lang.Math.pow(double a, double b)

You can use static import for this.

Pravat Panda
  • 1,060
  • 2
  • 13
  • 27