admin管理员组

文章数量:1317909

im using chrome with this code:

var startbet = 1;
var shot = 0;
var bet = startbet * 2^shot;

n^0=n in this case
it should be equal to 1 regardless of n

is this an error with javascript or do some people beleive to the power of 0 should be handled diffrently?

im using chrome with this code:

var startbet = 1;
var shot = 0;
var bet = startbet * 2^shot;

n^0=n in this case
it should be equal to 1 regardless of n

is this an error with javascript or do some people beleive to the power of 0 should be handled diffrently?

Share Improve this question edited Sep 24, 2012 at 6:36 Gaby_64 asked Sep 24, 2012 at 5:54 Gaby_64Gaby_64 777 bronze badges 4
  • What does "n^0=n in this case" mean? You haven't said anything about bet, startbet or shot. You need to clarify your question - at the moment it's unanswerable. – Jon Skeet Commented Sep 24, 2012 at 5:55
  • What is the value of startbet and what is the end value of bet that you're getting? – Dai Commented Sep 24, 2012 at 5:55
  • @Jon Skeet 2^0=2, 3^0=3, 4^0=4, i thought ^ meant 'to the power of' like in the windows calculator – Gaby_64 Commented Sep 24, 2012 at 6:39
  • @user1693548: You mean that's the Javascript behaviour - it wasn't clear what you expected vs what the actual behaviour was. – Jon Skeet Commented Sep 24, 2012 at 7:20
Add a ment  | 

2 Answers 2

Reset to default 9

In Javascript, the ^ operator is bitwise XOR, not exponent.

As Mikhail says, you have to use Math.pow() to pute exponents.

I think you need to use Math.pow instead

本文标签: exponentjavascript power of 0 odd resultStack Overflow