Bug 942550: Take care of all particular cases in ecmaPow instead of js_math_pow; r=jandem

This commit is contained in:
Benjamin Bouvier 2014-01-29 12:12:03 +01:00
parent 0466b1681e
commit cac8501966
2 changed files with 18 additions and 22 deletions

View File

@ -0,0 +1,6 @@
function pow(x,y) {
return Math.pow(x,y);
}
var x = pow(3, -.5);
var y = pow(3, -.5);
assertEq(x, y);

View File

@ -625,9 +625,21 @@ js::ecmaPow(double x, double y)
*/
if (!IsFinite(y) && (x == 1.0 || x == -1.0))
return GenericNaN();
/* pow(x, +-0) is always 1, even for x = NaN (MSVC gets this wrong). */
if (y == 0)
return 1;
/*
* Special case for square roots. Note that pow(x, 0.5) != sqrt(x)
* when x = -0.0, so we have to guard for this.
*/
if (IsFinite(x) && x != 0.0) {
if (y == 0.5)
return sqrt(x);
if (y == -0.5)
return 1.0 / sqrt(x);
}
return pow(x, y);
}
#if defined(_MSC_VER)
@ -651,29 +663,7 @@ js_math_pow(JSContext *cx, unsigned argc, Value *vp)
if (!ToNumber(cx, args.get(1), &y))
return false;
/*
* Special case for square roots. Note that pow(x, 0.5) != sqrt(x)
* when x = -0.0, so we have to guard for this.
*/
if (IsFinite(x) && x != 0.0) {
if (y == 0.5) {
args.rval().setNumber(sqrt(x));
return true;
}
if (y == -0.5) {
args.rval().setNumber(1.0/sqrt(x));
return true;
}
}
/* pow(x, +-0) is always 1, even for x = NaN. */
if (y == 0) {
args.rval().setInt32(1);
return true;
}
double z = ecmaPow(x, y);
args.rval().setNumber(z);
return true;
}