cvs: php4 /ext/standard math.c

From: Date: Sat, 04 Aug 2001 13:11:17 +0000
Subject: cvs: php4 /ext/standard math.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-6542@lists.php.net to get a copy of this message
jeroen Sat Aug 4 09:11:17 2001 EDT Modified files: /php4/ext/standard math.c Log: - A little bit cleaning of pow(), and 12 bytes less memory (wow) Index: php4/ext/standard/math.c diff -u php4/ext/standard/math.c:1.45 php4/ext/standard/math.c:1.46 --- php4/ext/standard/math.c:1.45 Fri Aug 3 19:27:23 2001 +++ php4/ext/standard/math.c Sat Aug 4 09:11:17 2001 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: math.c,v 1.45 2001/08/03 23:27:23 jeroen Exp $ */ +/* $Id: math.c,v 1.46 2001/08/04 13:11:17 jeroen Exp $ */ #include "php.h" #include "php_math.h" @@ -378,8 +378,8 @@ /* }}} */ -/* {{{ 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 @@ 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 @@ 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 @@ 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 @@ 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 */ } }

« previous php.cvs (#6542) next »