Math.pow with negative numbers and non-integer powers

后端 未结 2 1928
予麋鹿
予麋鹿 2020-12-10 05:02

The ECMAScript specification for Math.pow has the following peculiar rule:

  • If x < 0 and x is finite and y is finite and y is n
2条回答
  •  -上瘾入骨i
    2020-12-10 05:51

    You can use a helper function.

    In swift I faced a similar situation. Here is a proposed solution for you:

    function checkSquareRoot(x, y) {
        if (x > 0) {
            return Math.pow(x, y)
        }
        return -1 * Math.pow(-x, y)
    }
    

提交回复
热议问题