iliaa           Mon Nov 17 21:50:52 2003 EDT

  Added files:                 (Branch: PHP_4_3)
    /php-src/ext/standard/tests/math    bug25694.phpt 

  Modified files:              
    /php-src/ext/standard       math.c 
    /php-src    NEWS 
  Log:
  MFH: Fixed Bug #25694 (round() and number_format() give inconsistent 
  results).
  
  
Index: php-src/ext/standard/math.c
diff -u php-src/ext/standard/math.c:1.97.2.7 php-src/ext/standard/math.c:1.97.2.8
--- php-src/ext/standard/math.c:1.97.2.7        Thu Aug 28 12:01:49 2003
+++ php-src/ext/standard/math.c Mon Nov 17 21:50:49 2003
@@ -19,7 +19,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: math.c,v 1.97.2.7 2003/08/28 16:01:49 sas Exp $ */
+/* $Id: math.c,v 1.97.2.8 2003/11/18 02:50:49 iliaa Exp $ */
 
 #include "php.h"
 #include "php_math.h"
@@ -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 @@
 {
        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 @@
                        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 @@
        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])) {
Index: php-src/NEWS
diff -u php-src/NEWS:1.1247.2.470 php-src/NEWS:1.1247.2.471
--- php-src/NEWS:1.1247.2.470   Mon Nov 17 14:32:03 2003
+++ php-src/NEWS        Mon Nov 17 21:50:50 2003
@@ -29,6 +29,8 @@
 - 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)
 

Index: php-src/ext/standard/tests/math/bug25694.phpt
+++ php-src/ext/standard/tests/math/bug25694.phpt
--TEST--
Bug #25694 (round() and number_format() inconsistency)
--FILE--
<?php
echo "round 0.045 = " . round(0.045, 2) . "\n";
echo "number format 0.045 = " . number_format(0.045, 2) . "\n\n";
echo "round 0.055 = " . round(0.055, 2) . "\n";
echo "number format 0.055 = " . number_format(0.055, 2) . "\n\n";
echo "round 5.045 = " . round(5.045, 2) . "\n";
echo "number format 5.045 = " . number_format(5.045, 2) . "\n\n";
echo "round 5.055 = " . round(5.055, 2) . "\n";
echo "number format 5.055 = " . number_format(5.055, 2) . "\n\n";
echo "round 3.025 = " . round(3.025, 2) . "\n";
echo "number format 3.025 = " . number_format(3.025, 2) . "\n\n";
echo "round 4.025 = " . round(4.025, 2) . "\n";
echo "number format 4.025 = " . number_format(4.025, 2) . "\n\n";
?>
--EXPECT--
round 0.045 = 0.05
number format 0.045 = 0.05

round 0.055 = 0.06
number format 0.055 = 0.06

round 5.045 = 5.05
number format 5.045 = 5.05

round 5.055 = 5.06
number format 5.055 = 5.06

round 3.025 = 3.03
number format 3.025 = 3.03

round 4.025 = 4.03
number format 4.025 = 4.03

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to