ID:               25328
 Updated by:       [EMAIL PROTECTED]
 Reported By:      hakon at haugnes dot name
-Status:           Open
+Status:           Bogus
 Bug Type:         *Math Functions
 Operating System: Redhat 8.0
 PHP Version:      4.3.2
 New Comment:

It's not a malfunction, PHP simply doesn't support integer numbers over
2^31 - 1 and will convert them to a float. And yes, you should use the
bc* functions for this.

Derick


Previous Comments:
------------------------------------------------------------------------

[2003-08-30 19:06:09] hakon at haugnes dot name

I just realized that the [mal]functioning of % for large numbers is
probably why the function BCMOD has been created...

------------------------------------------------------------------------

[2003-08-30 18:39:00] hakon at haugnes dot name

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 this bug report at http://bugs.php.net/?id=25328&edit=1

Reply via email to