php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37082 strange comparison on 64bit AMD
Submitted: 2006-04-14 13:05 UTC Modified: 2006-04-14 15:20 UTC
From: oliver at teqneers dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.1.2 OS: Suse Linux 10/Debian Sarge
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: oliver at teqneers dot de
New email:
PHP Version: OS:

 

 [2006-04-14 13:05 UTC] oliver at teqneers dot de
Description:
------------
When operating at or beyond the integer limits, there is a different behaviour between a 32bit and 64bit processor (beside the different max value of course).

on a 32bit processor the limit is 2147483647, on a 64bit it is 9223372036854775807.

if i try to compare a STRING value like "9223372036854775807" with a string value "9223372036854775808", which is higher, than the integer limit, the comparison between those two returns true, but it should be false.

This problem seems also to exists in PHP 4.4.x.

Reproduce code:
---------------
64-BIT-Source:
--------------
<?

echo 'max<br>';
echo PHP_INT_MAX.'<br>';

echo 'intval<br>';
echo intval(9223372036854775807).'<br>';
echo intval(9223372036854775808).'<br>';
echo intval(9223372036854775809).'<br>';
echo intval(9223372036854776832).'<br>';
echo intval(9223372036854776833).'<br>';

echo 'intval of string<br>';
echo intval('9223372036854775807').'<br>';
echo intval('9223372036854775808').'<br>';
echo intval('9223372036854775809').'<br>';
echo intval('9223372036854776832').'<br>';
echo intval('9223372036854776833').'<br>';

echo 'compares<br>';
if( '9223372036854775808' == '9223372036854775807' ) {
	echo 'wrong<br>';
}
if( '9223372036854775808' == '9223372036854775806' ) {
	echo 'wrong<br>';
}
if( 9223372036854775808 == 9223372036854775806 ) {
	echo 'wrong<br>';
}
if( strval('9223372036854775808') == strval('9223372036854775806') ) {
	echo 'wrong<br>';
}
if( (string)'9223372036854775808' == (string)'9223372036854775806' ) {
	echo 'wrong<br>';
}
if( (string)'9323372036854775806' == (string)'9223372036854775806' ) {
	echo 'works fine<br>';
}

?>

32-BIT-Source:
--------------
<?

echo 'max<br>';
echo PHP_INT_MAX.'<br>';

echo 'intval<br>';
echo intval(2147483647).'<br>';
echo intval(2147483648).'<br>';
echo intval(2147483649).'<br>';
echo intval(2147483650).'<br>';
echo intval(2147483651).'<br>';

echo 'intval of string<br>';
echo intval('2147483647').'<br>';
echo intval('2147483648').'<br>';
echo intval('2147483649').'<br>';
echo intval('2147483650').'<br>';
echo intval('2147483651').'<br>';

echo 'compares<br>';
if( '2147483648' == '2147483647' ) {
        echo 'wrong<br>';
}
if( '2147483648' == '2147483646' ) {
        echo 'wrong<br>';
}
if( 2147483648 == 2147483646 ) {
        echo 'wrong<br>';
}
if( strval('2147483648') == strval('2147483646') ) {
        echo 'wrong<br>';
}
if( (string)'2147483648' == (string)'2147483646' ) {
        echo 'wrong<br>';
}

?>


Expected result:
----------------
should be more or less equal to the 32-bit output, except of a higher number of course.

Actual result:
--------------
64-BIT-Source:
--------------
max
9223372036854775807
intval
9223372036854775807
-9223372036854775808
-9223372036854775808
-9223372036854775808
-9223372036854773760
intval of string
9223372036854775807
9223372036854775807
9223372036854775807
9223372036854775807
9223372036854775807
compares
wrong
wrong
wrong
wrong
wrong


32-BIT-Source:
--------------
max
PHP_INT_MAX
intval
2147483647
-2147483648
-2147483647
-2147483646
-2147483645
intval of string
2147483647
2147483647
2147483647
2147483647
2147483647
compares


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-14 14:51 UTC] tony2001@php.net
>if( (string)'9323372036854775806' == (string)'9223372036854775806' )
Adding (string) here doesn't make any sense - they ARE strings.
And numeric strings are converted to integers/floats automatically.

Both var_dump(9323372036854775808); and var_dump(932337203685477580); produce the same result and their equality is the correct result.
Furthermore, this is something to do with the system (which is unable to compare big floats adequately), not with PHP.
No PHP bug here.
 [2006-04-14 15:04 UTC] oliver at teqneers dot de
This is not quite correct, because var_dump(9323372036854775808);
var_dump(9323372036854775806);
return both float values.
The problem here is, that the second value is NOT a float, but an integer value because it is still in the range of a 64bit integer.

And the following expression should still not return true
vd('9223372036854775808' == '9223372036854775806')

If this is not a PHP issue, where should I look into? Compiler, OS, ...?
 [2006-04-14 15:09 UTC] tony2001@php.net
>This is not quite correct, because var_dump
>(9323372036854775808); var_dump(9323372036854775806); 
>return both float values.

Sure. 
Because 9323372036854775806 is MUCH bigger than 9223372036854775806.
Note "93.." and "92.." in the beginning.
 [2006-04-14 15:20 UTC] oliver at teqneers dot de
very sorry for the typo. now i get a float and int as well...

but why is the following expression on a 64bit true?
var_dump('9223372036854775808' == '9223372036854775806');
and this following expression on a 32-bit false?
var_dump('2147483648' == '2147483646');

on both systems one of the values var_dumps to float and the other to int???
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 08:01:32 2025 UTC