how about more args for Math.hypot
roger.andrews at mail104.co.uk
Thu Mar 15 06:45:06 PDT 2012
> Replying to the four proposals you sent in one mail for easier discussion:
OK. I've replied to your replies in this thread as 4 short posts.
> *** Math.cbrt ***
> This was considered, but wasn't above the line of what was in the proposed
> Math extensions. What use case do you have in mind?
I don't have specific use cases, but observe that cbrt is very useful in
maths - comparable with sqrt. Cubic equations seem to be nearly as frequent
as quadratic (and can be solved by Cardano's Method like the famous
Comparing against Math.pow:
1) cbrt is more explicit about -0 and -Infinity, whereas pow deals with the
general case. (Note that sqrt is also explicit about -0.)
2) cbrt correctly handles a negative domain (x<0) with no nasty drama,
whereas pow returns NaN - forcing the user to check and correct before
3) Obviously, cbrt is more accurate than pow since 1/3 is not exact in
floating-point, and the error compounds internally.
I offer the following implementation of Math.cbrt ...
Math.cbrt = function (x)
x = +x;
// handle NaN, infinities, zeroes -- thus avoid Inf/Inf and 0/0
if (isNaN( x/x )) return x;
// estimate the cube root well;
// then improve by Halley's Method (more accurate ULP than Newton's!)
var r = Math.pow( Math.abs(x), 1/3 );
var t = x/r/r; // safe!
return r + (r * (t-r) / (2*r + t));
More information about the es-discuss