#42336 [Opn]: A bug on rounding and calculating numbers?
| From: | Makinen_Juha at hotmail dot com | Date: | Sat, 18 Aug 2007 12:21:12 +0000 |
| Subject: | #42336 [Opn]: A bug on rounding and calculating numbers? | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-116301@lists.php.net to get a copy of this message | ||
ID: 42336
User updated by: Makinen_Juha at hotmail dot com
Reported By: Makinen_Juha at hotmail dot com
Status: Open
Bug Type: *Math Functions
Operating System: winxp/2000
PHP Version: 5.2.3
New Comment:
Round fails at 1.255, not 1.225 :)
Previous Comments:
------------------------------------------------------------------------
[2007-08-18 11:32:15] Makinen_Juha at hotmail dot com
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 this bug report at http://bugs.php.net/?id=42336&edit=1