Math method to calculate root for arbitrary exponent (e.g. cube root)

Hello.

It would be nice to have a standard method to calculate arbitrary root (e.g. cube root).

Currently similar result can be achieved with Math.pow(x, 1/n) where x is source number, and n is exponent. But that does not work with negative x and returns NaN in all browsers:

    Math.pow(-8, 1/3) // -2 is expected, NaN is returned.

So probably either Math.pow() should be improved to take negative numbers into account, or a new method like Math.root(x, n) should be added.

While, of course, the issue can be _currently_ worked around with a pure-script solution taking negative numbers into account, this proposal is exactly about improving _core JavaScript functionality_ to provide _standard_ means for the purpose.

Thanks.

Received on Sunday, 29 January 2012 23:49:01 UTC