Argh. It's just floating point inaccuracy. While I think Math.pow isn't as accurate as it could be, floating point maths will always cause problems like this. In this case jsconsole is right and Espruino gets it wrong, but I bet if you looked hard enough you could find similar cases where it did something strange too.
If you do some floating point operation it probably won't be entirely accurate, and it's 50/50 whether it'll be above the correct number or below it. If you drop the fractional bits, even if the number was 15.99999999999 you'll still just get 15.
For now, if you just do 16&Math.round(Math.pow(2,4)) it'll fix it.
I could add some code that was a bit like: if (is_not_fractional(a) && is_not_fractional(b) && b>0) return round(Math.pow(a,b)); but it's a bit of a hack and I wonder whether it'll cause problems later.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Argh. It's just floating point inaccuracy. While I think
Math.pow
isn't as accurate as it could be, floating point maths will always cause problems like this. In this case jsconsole is right and Espruino gets it wrong, but I bet if you looked hard enough you could find similar cases where it did something strange too.If you do some floating point operation it probably won't be entirely accurate, and it's 50/50 whether it'll be above the correct number or below it. If you drop the fractional bits, even if the number was 15.99999999999 you'll still just get 15.
For now, if you just do
16&Math.round(Math.pow(2,4))
it'll fix it.I could add some code that was a bit like:
if (is_not_fractional(a) && is_not_fractional(b) && b>0) return round(Math.pow(a,b));
but it's a bit of a hack and I wonder whether it'll cause problems later.