Math.pow with negative numbers and non-integer powers
I assume because those circumstances lead the result into complex waters, and ECMAScript is not equipped with imaginary numbers. Specifically, your example should result in something close to 1 + 1.732i
, among other results. (The fact that -2 is also a possible result is besides the point - it is an accident rather than a rule.)
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)
}