DEV Community

Discussion on: Square a number: awful answers only

Collapse
 
ferow2k profile image
Fernando Wentland

Just ignore the floating point error:

const NUMBER_OF_SIDES = 2;
function square(value) {
  return Math.sign(value) * Math.exp(NUMBER_OF_SIDES * Math.log(Math.abs(value)));
}
Enter fullscreen mode Exit fullscreen mode