From:             Makinen_Juha at hotmail dot com
Operating system: winxp/2000
PHP version:      5.2.3
PHP Bug Type:     *Math Functions
Bug description:  A bug on rounding and calculating numbers?

Description:
------------
In the code below, i get wrong results with php 5.2.3 and 5.2.2.

The code tries to round 3 digit numbers to 2 digit numbers. Round fails at
1.225 and calculated numbers at 4.264.

Iam creating a big calculating software with this language and i hope this
is my mistake :)

Reproduce code:
---------------
<?php
        for ( $i=0.01;$i<10;$i=$i+0.003 )
        { 
                echo $i." -> ".calcRound( $i )."<br>";
                echo round($i,2)."<br>";
        }

        function calcRound( $inNumber )
        {
                $intnum = $inNumber*100;
                $intnum = (int) $intnum;

                $decnum = $inNumber*100;

                if ( $decnum - $intnum > 0.4 ) { $intnum++; }

                return $intnum / 100;
        }
?>

Expected result:
----------------
...
1.255 -> 1.26 (calculated round)
1.26 (round function)
...

...
4.264 -> 4.26 (calculated round)
4.26 (with round function)
4.267 -> 4.27 (calculated round)
...

Actual result:
--------------
...
1.255 -> 1.26 (calculated round)
1.25 (round function)
...

...
4.264 -> 4.27 (calculated round)
4.26 (with round function)
4.2670000000001 -> 4.27 (calculated round)
...

-- 
Edit bug report at http://bugs.php.net/?id=42336&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=42336&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=42336&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=42336&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=42336&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=42336&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=42336&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=42336&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=42336&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=42336&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=42336&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=42336&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=42336&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=42336&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=42336&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=42336&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=42336&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=42336&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=42336&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=42336&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=42336&r=mysqlcfg

Reply via email to