#36730 [NEW]: int and intval produce wrong results
| From: | scott dot fagg at arup dot com | Date: | Tue, 14 Mar 2006 00:20:47 +0000 |
| Subject: | #36730 [NEW]: int and intval produce wrong results | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-94315@lists.php.net to get a copy of this message | ||
From: scott dot fagg at arup dot com
Operating system: linux
PHP version: 4.4.2
PHP Bug Type: *Math Functions
Bug description: int and intval produce wrong results
Description:
------------
(int) and intval() generate wrong values.
Output of (int) and intval() is affected by how the values passed to them,
were calculated.
Reproduce code:
---------------
$x = 0.57 ;
$a = (int) ($x * 100);
$b = intval($x * 100);
# produce incorrect results , $b = 56
print "$x $a $b\n";
$x = 0.56 + 0.01;
$a = (int) ($x * 100);
$b = intval($x * 100);
# produce expected results , $b = 57
print "$x $a $b\n";
Expected result:
----------------
(int) and intval() should produce correct answers and should not be
affected by how the values passed to them are calculated.
Workaround seems to be to use round() instead of intval().
Actual result:
--------------
Above code produces :
0.57 56 56
0.57 57 57
--
Edit bug report at http://bugs.php.net/?id=36730&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=36730&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): http://bugs.php.net/fix.php?id=36730&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=36730&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=36730&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=36730&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=36730&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=36730&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=36730&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=36730&r=support
Expected behavior: http://bugs.php.net/fix.php?id=36730&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=36730&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=36730&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=36730&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=36730&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=36730&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=36730&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=36730&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=36730&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=36730&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=36730&r=mysqlcfg