From: Ilia Alshanetsky Date: Tue, 18 Nov 2003 02:50:50 +0000 (+0000) Subject: MFH: Fixed Bug #25694 (round() and number_format() give inconsistent X-Git-Tag: php-4.3.5RC1~193 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5e6d8ed2d15783784afc4b45c0decb02b8b53318;p=php MFH: Fixed Bug #25694 (round() and number_format() give inconsistent results). --- diff --git a/NEWS b/NEWS index 6de0207ba0..a98f841989 100644 --- a/NEWS +++ b/NEWS @@ -29,6 +29,8 @@ PHP 4 NEWS - Fixed bug #26003 (Make fgetcsv() binary safe). (Ilia, Moriyoshi) - Fixed bug #25794 (Cannot open existing hash db3 file with write" ext/dba). (Marcus) +- Fixed Bug #25694 (round() and number_format() give inconsistent results). + (Ilia) - Fixed bug #25581 (getimagesize () return incorrect values on bitmap (os2) files). (Marcus) diff --git a/ext/standard/math.c b/ext/standard/math.c index e062c923ef..16dfb355fd 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -40,6 +40,18 @@ # endif #endif +#define PHP_ROUND_WITH_FUZZ(val, places) { \ + double tmp_val=val, f = pow(10.0, (double) places); \ + tmp_val *= f; \ + if (tmp_val >= 0.0) { \ + tmp_val = floor(tmp_val + PHP_ROUND_FUZZ); \ + } else { \ + tmp_val = ceil(tmp_val - PHP_ROUND_FUZZ); \ + } \ + tmp_val /= f; \ + val = !zend_isnan(tmp_val) ? tmp_val : val; \ +} \ + /* {{{ proto int abs(int number) Return the absolute value of the number */ @@ -121,7 +133,7 @@ PHP_FUNCTION(round) { zval **value, **precision; int places = 0; - double f, return_val; + double return_val; if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &value, &precision) == FAILURE) { @@ -147,14 +159,7 @@ PHP_FUNCTION(round) return_val = (Z_TYPE_PP(value) == IS_LONG) ? (double)Z_LVAL_PP(value) : Z_DVAL_PP(value); - f = pow(10.0, (double) places); - - return_val *= f; - if (return_val >= 0.0) - return_val = floor(return_val + PHP_ROUND_FUZZ); - else - return_val = ceil(return_val - PHP_ROUND_FUZZ); - return_val /= f; + PHP_ROUND_WITH_FUZZ(return_val, places); RETURN_DOUBLE(return_val); break; @@ -995,13 +1000,15 @@ PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char tho int tmplen, reslen=0; int count=0; int is_negative=0; - + if (d < 0) { is_negative = 1; d = -d; } dec = MAX(0, dec); + PHP_ROUND_WITH_FUZZ(d, dec); + tmplen = spprintf(&tmpbuf, 0, "%.*f", dec, d); if (tmpbuf == NULL || !isdigit((int)tmpbuf[0])) {