Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript power of 0 odd result?

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?

like image 880
Gaby_64 Avatar asked Dec 15 '22 19:12

Gaby_64


2 Answers

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

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

like image 192
Frédéric Hamidi Avatar answered Dec 18 '22 09:12

Frédéric Hamidi


I think you need to use Math.pow instead

like image 29
Mikhail Payson Avatar answered Dec 18 '22 09:12

Mikhail Payson