Skip to content

Instantly share code, notes, and snippets.

@ryanwoodsmall
Last active March 29, 2017 01:54
Show Gist options
  • Save ryanwoodsmall/80d5323ba59c52db2b8417db7cc19322 to your computer and use it in GitHub Desktop.
Save ryanwoodsmall/80d5323ba59c52db2b8417db7cc19322 to your computer and use it in GitHub Desktop.
nasty buggy javascript exponent thing
function myexpt(x, y) {
var expt = 1;
if (x < 0) {
// XXX - need to check Number.isFinite(parseFloat(y)) and cast for modulus via parseInt
if (y % 2 != 0) {
expt = -1 * myexpt(Math.abs(x), y);
} else {
expt = myexpt(Math.abs(x), y);
}
} else {
if (y < 0) {
expt = 1 / myexpt(x, Math.abs(y));
} else if (y == 0) {
expt = 1;
} else if (y > 0) {
expt = x * myexpt(x, y - 1);
}
}
return(expt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment