#38045 [NEW]: Integer mathematics problem
| From: | igor at oleinikov dot ru | Date: | Sun, 09 Jul 2006 09:30:38 +0000 |
| Subject: | #38045 [NEW]: Integer mathematics problem | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-99246@lists.php.net to get a copy of this message | ||
From: igor at oleinikov dot ru
Operating system: FreeBSD 6.1 Release
PHP version: 5.1.4
PHP Bug Type: Scripting Engine problem
Bug description: Integer mathematics problem
Description:
------------
When I want to calculate integer summ of two integers with overflow, I get
unexpected result, becouse result converts to float, then converts to
integer.
Important: I've tested this example on two systems with same versions of
php (5.1.4) and different versions of freebsd (4.11 and 6.1).
On last operation [$b = (int)($a + $a)], freebsd 4.11 produce expected
result: int(0), but freebsd 6.1 produce minimal integer (-2147483648).
How can I implement algorithm with integer mathematics, that requires
overflows? i have it successfully working on freebsd 4.11, but it don't
work on freebsd 6.1 ((
Reproduce code:
---------------
$a = 0x80000000; //i'd like it would integer, but it float ;)
var_dump($a, dechex($a));
$a = (int)0x80000000; //ok, i'll make it integer myself
var_dump($a, dechex($a));
$b = $a + $a; //int + int = int ?
var_dump($b, dechex($b));
$b = (int)($a + $a);
var_dump($b, dechex($b));
Expected result:
----------------
int(-2147483648) string(8) "80000000"
int(-2147483648) string(8) "80000000"
int(0) string(8) "0" //expected, int + int = int with overflow
int(0) string(8) "0" //expected, conversion with overflow
Actual result:
--------------
float(2147483648) string(8) "80000000" // expected int
int(-2147483648) string(8) "80000000" // when I convert to int myself
float(-4294967296) string(8) "80000000" //expected int, but it overflows
and converts to float
int(-2147483648) string(8) "80000000"
--
Edit bug report at http://bugs.php.net/?id=38045&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=38045&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=38045&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=38045&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=38045&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=38045&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=38045&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=38045&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=38045&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=38045&r=support
Expected behavior: http://bugs.php.net/fix.php?id=38045&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=38045&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=38045&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=38045&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=38045&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=38045&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=38045&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=38045&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=38045&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=38045&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=38045&r=mysqlcfg