4
(((difference - previousStep)/1000)^2)
//difference and previousStep are both doubles

Why can't I use the ^ operator with doubles? I just want to know why. Luckily for me I can just multiple difference - previousStep by itself because i'm just squaring it, but if i need to bring it to the Nth power, then this would be a problem. So why can't you ^ doubles and is there a way around this?

MagnusCaligo
  • 707
  • 2
  • 8
  • 28
  • The caret (^) operator is explained in another SO question [^ operator in java][1] [1]: http://stackoverflow.com/questions/460542/operator-in-java – user3681234 May 28 '14 at 02:31

2 Answers2

10

The ^ operator isn't the exponentiation operator in Java; it's the bitwise XOR operator, which doesn't make much sense with double arguments.

You can either multiply the value by itself or you can call Math.pow(yourValue, 2) for exponentiation.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

There operator ^ is not used for exponents in Java, please use Math.pow(((difference - previousStep)/1000), 2) instead

Victor2748
  • 4,149
  • 13
  • 52
  • 89