From:             jeff at tmtrading dot com
Operating system: Redhat 8.0
PHP version:      4CVS-2003-07-30 (stable)
PHP Bug Type:     Math related
Bug description:  Round does not round properly

Description:
------------
php: php4-STABLE-200307301930

gcc: 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

I tried to model the "myround" function after the c code.
I am not a C coder but I managed to get php to output the correct result
with the following _hack_.

--- php/ext/standard/math.c     2003-01-16 07:08:59.000000000 -0700
+++ php/ext/standard/math.c     2003-07-30 16:23:44.000000000 -0700
@@ -114,6 +114,7 @@
        zval **value, **precision;
        int places = 0;
        double f, return_val;
+       float fixme_val;

        if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 ||
                zend_get_parameters_ex(ZEND_NUM_ARGS(), &value,
&precision) == FAILURE) {
@@ -142,10 +143,11 @@
                        f = pow(10.0, (double) places);

                        return_val *= f;
+                       fixme_val = (float) return_val;
                        if (return_val >= 0.0)
-                               return_val = floor(return_val + 0.5);
+                               return_val = floor((double)(fixme_val +
0.5));
                        else
-                               return_val = ceil(return_val - 0.5);
+                               return_val = ceil((double)(fixme_val -
0.5));
                        return_val /= f;

                        RETURN_DOUBLE(return_val);


As you can see the patch is lame.  Since I am not a C programmer I do not
know how to figure out why this works vs. the original (it appears
correct!).


Reproduce code:
---------------
$numbers = array(4.045, 5.055, 41.045, 51.055);

foreach($numbers as $number) {
    printf("%f : %f : %f\n",$number, round($number,2),
myround($number,2));
}

function myround($value, $precision) {
  (int) $places = $precision;
  (double) $f;
  (double) $return_val = (double) $value;

  $f = pow(10.0, (double) $places);
  $return_val *= $f;

  if($return_val >= 0.0)
    $return_val = floor($return_val + 0.5);
  else
    $return_val = ceil($return_val - 0.5);

  $return_val /= $f;
  return $return_val;
}

Expected result:
----------------
[EMAIL PROTECTED] cli]$ ./php -n -f test.php
4.045000 : 4.050000 : 4.050000
5.055000 : 5.060000 : 5.060000
41.045000 : 41.050000 : 41.050000
51.055000 : 51.060000 : 51.060000


Actual result:
--------------
[EMAIL PROTECTED] cli]$ ./php -n -f ./test.php
4.045000 : 4.040000 : 4.050000
5.055000 : 5.050000 : 5.060000
41.045000 : 41.050000 : 41.050000
51.055000 : 51.050000 : 51.060000


-- 
Edit bug report at http://bugs.php.net/?id=24876&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=24876&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=24876&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=24876&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=24876&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=24876&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=24876&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=24876&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=24876&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=24876&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=24876&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=24876&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=24876&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=24876&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=24876&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=24876&r=gnused

Reply via email to