mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
better power implementation
This commit is contained in:
parent
cf3d1087d1
commit
39739ea0ed
1 changed files with 54 additions and 18 deletions
|
@ -316,20 +316,55 @@ float_divmod(v, w)
|
||||||
return mkvalue("(dd)", div, mod);
|
return mkvalue("(dd)", div, mod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double powu(x, n)
|
||||||
|
double x;
|
||||||
|
long n;
|
||||||
|
{
|
||||||
|
double r = 1.;
|
||||||
|
double p = x;
|
||||||
|
long mask = 1;
|
||||||
|
while (mask > 0 && n >= mask) {
|
||||||
|
if (n & mask)
|
||||||
|
r *= p;
|
||||||
|
mask <<= 1;
|
||||||
|
p *= p;
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
double powi(x, n)
|
||||||
|
double x;
|
||||||
|
long n;
|
||||||
|
{
|
||||||
|
if (n > 10000 || n < -10000)
|
||||||
|
return pow(x, (double) n);
|
||||||
|
else if (n > 0)
|
||||||
|
return powu(x, n);
|
||||||
|
else
|
||||||
|
return 1./powu(x, -n);
|
||||||
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
float_pow(v, w, z)
|
float_pow(v, w, z)
|
||||||
floatobject *v;
|
floatobject *v;
|
||||||
floatobject *w;
|
object *w;
|
||||||
floatobject *z;
|
floatobject *z;
|
||||||
{
|
{
|
||||||
double iv, iw, ix;
|
double iv, iw, ix;
|
||||||
iv = v->ob_fval;
|
long intw;
|
||||||
iw = w->ob_fval;
|
|
||||||
/* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
|
/* XXX Doesn't handle overflows if z!=None yet; it may never do so :(
|
||||||
* The z parameter is really only going to be useful for integers and
|
* The z parameter is really only going to be useful for integers and
|
||||||
* long integers. Maybe something clever with logarithms could be done.
|
* long integers. Maybe something clever with logarithms could be done.
|
||||||
* [AMK]
|
* [AMK]
|
||||||
*/
|
*/
|
||||||
|
iv = v->ob_fval;
|
||||||
|
iw = ((floatobject *)w)->ob_fval;
|
||||||
|
intw = (long)iw;
|
||||||
|
if (iw == intw) {
|
||||||
|
errno = 0;
|
||||||
|
ix = powi(iv, intw);
|
||||||
|
}
|
||||||
|
else {
|
||||||
/* Sort out special cases here instead of relying on pow() */
|
/* Sort out special cases here instead of relying on pow() */
|
||||||
if (iw == 0.0) { /* x**0 is 1, even 0**0 */
|
if (iw == 0.0) { /* x**0 is 1, even 0**0 */
|
||||||
if ((object *)z!=None) {
|
if ((object *)z!=None) {
|
||||||
|
@ -348,6 +383,7 @@ float_pow(v, w, z)
|
||||||
}
|
}
|
||||||
errno = 0;
|
errno = 0;
|
||||||
ix = pow(iv, iw);
|
ix = pow(iv, iw);
|
||||||
|
}
|
||||||
CHECK(ix);
|
CHECK(ix);
|
||||||
if (errno != 0) {
|
if (errno != 0) {
|
||||||
/* XXX could it be another type of error? */
|
/* XXX could it be another type of error? */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue