From:             hakon at haugnes dot name
Operating system: Redhat 8.0
PHP version:      4.3.2
PHP Bug Type:     *Math Functions
Bug description:  modulus operator problem with large numbers

Description:
------------
The modulus operator % returns wrong results if the number gets large. In
a program used to calculate primes, it will find that large numbers,
ending in 5, are prime. The following are calculations using % on such a
large number, note that all numbers used in the MOD calculation are whole
integers (and should not suffer rounding by MOD):

MOD: 53253252355 % 3=1 
DIV: 53253252355 / 3=17751084118.333332061767578125

MOD: 53253252355 % 4=3 
DIV: 53253252355 / 4=13313313088.75

MOD: 53253252355 % 5=2 
DIV: 53253252355 / 5=10650650471

Obviously, any number ending in 5 cannot be prime, since it will always be
divisible by 5. The MOD result on 5 is therefore wrong, as the division
proves.

Reproduce code:
---------------
$i=53253252355;
ini_set("precision",35);

for ($m=2; $m<=$i; $m++){
 echo "MOD: $i % $m=". $i % $m ;
 echo "DIV: $i / $m=". $i/$m ."<Br>";     
 }

Expected result:
----------------
I would expect to see 

MOD: 53253252355 % 3=1 
DIV: 53253252355 / 3=17751084118.333332061767578125
MOD: 53253252355 % 4=3 
DIV: 53253252355 / 4=13313313088.75
MOD: 53253252355 % 5=0 
DIV: 53253252355 / 5=10650650471

Actual result:
--------------
However, this is produced: ([THE ERROR] added for your convenience)

MOD: 53253252355 % 3=1 
DIV: 53253252355 / 3=17751084118.333332061767578125
MOD: 53253252355 % 4=3 
DIV: 53253252355 / 4=13313313088.75
MOD: 53253252355 % 5=2 [THE ERROR]
DIV: 53253252355 / 5=10650650471


-- 
Edit bug report at http://bugs.php.net/?id=25328&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=25328&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=25328&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=25328&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=25328&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=25328&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=25328&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=25328&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=25328&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=25328&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=25328&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=25328&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=25328&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=25328&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=25328&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=25328&r=gnused

Reply via email to