summaryrefslogtreecommitdiff
path: root/ext/standard/math.c
diff options
context:
space:
mode:
authorJeroen van Wolffelaar <jeroen@php.net>2001-08-04 13:11:17 +0000
committerJeroen van Wolffelaar <jeroen@php.net>2001-08-04 13:11:17 +0000
commit8803bbaa804d214109f527154b1e67edf2f64e92 (patch)
treec9da005650765a15a8ef2bb4f9d41b1e87291499 /ext/standard/math.c
parentfff883689ec2b0313b8ff4a920078c3e8822ca54 (diff)
downloadphp-git-8803bbaa804d214109f527154b1e67edf2f64e92.tar.gz
- A little bit cleaning of pow(), and 12 bytes less memory (wow)
Diffstat (limited to 'ext/standard/math.c')
-rw-r--r--ext/standard/math.c26
1 files changed, 14 insertions, 12 deletions
diff --git a/ext/standard/math.c b/ext/standard/math.c
index e7b5663b7e..1a448ac02c 100644
--- a/ext/standard/math.c
+++ b/ext/standard/math.c
@@ -378,8 +378,8 @@ PHP_FUNCTION(pi)
/* }}} */
-/* {{{ proto number pow(number lbase, number lexponent)
- Returns lbase raised to the power of lexponent. Returns
+/* {{{ proto number pow(number base, number exponent)
+ Returns base raised to the power of exponent. Returns
integer result when possible. */
PHP_FUNCTION(pow)
@@ -390,8 +390,7 @@ PHP_FUNCTION(pow)
zval **zbase, **zexp;
long lbase,lexp;
- double dval,dbase,pos_result;
- long result = 1;
+ double dval;
if (ZEND_NUM_ARGS() != 2) {
WRONG_PARAM_COUNT;
@@ -407,15 +406,15 @@ PHP_FUNCTION(pow)
if (Z_TYPE_PP(zexp) == IS_DOUBLE) {
/* pow(?,float), this is the ^^ case */
- dbase = Z_TYPE_PP(zbase) == IS_LONG ? (double) Z_LVAL_PP(zbase)
- : Z_DVAL_PP(zbase);
- if ( dbase <= 0.0 ) {
+ convert_to_double_ex(zbase);
+
+ if ( Z_DVAL_PP(zbase) <= 0.0 ) {
/* Note that with the old behaviour, php pow() returned bogus
results. Try pow(-1,2.5) in PHP <= 4.0.6 ... */
php_error(E_WARNING,"Trying to raise a nonpositive value to a broken power");
RETURN_FALSE;
}
- RETURN_DOUBLE(exp(log(dbase) * Z_DVAL_PP(zexp)));
+ RETURN_DOUBLE(exp(log(Z_DVAL_PP(zbase)) * Z_DVAL_PP(zexp)));
}
/* pow(?,int), this is the ** case */
@@ -439,8 +438,8 @@ PHP_FUNCTION(pow)
RETURN_DOUBLE(0.0);
}
} else { /* lbase < 0.0 */
- pos_result = exp(log(-Z_DVAL_PP(zbase)) * (double)lexp);
- RETURN_DOUBLE(lexp & 1 ? -pos_result : pos_result);
+ dval = exp(log(-Z_DVAL_PP(zbase)) * (double)lexp);
+ RETURN_DOUBLE(lexp & 1 ? -dval : dval);
}
}
@@ -475,14 +474,17 @@ PHP_FUNCTION(pow)
RETURN_DOUBLE(((lexp & 1) && lbase<0) ? -dval : dval);
}
+ Z_TYPE_P(return_value) = IS_LONG;
+ Z_LVAL_P(return_value) = 1;
+
/* loop runs at most log(log(LONG_MAX)) times, i.e. ~ 5 */
while (lexp > 0) {
if (lexp & 1) /* odd */
- result *= lbase;
+ Z_LVAL_P(return_value) *= lbase;
lexp >>= 1;
lbase *= lbase;
}
- RETURN_LONG(result);
+ /* return */
}
}