0

I'm using bcmod and gmp_mod functions in php for handling large numbers.

This works fine:

// large number must be string
$n = "10000000000000000000001";
$y = 1025;
$c = 1025;
// Both works the same (also tested in python)
$y = gmp_mod( (bcpowmod($y, 2, $n) + $c) , $n);
$y = bcmod  ( (bcpowmod($y, 2, $n) + $c) , $n);

But the input $n is not static. So I must use type casting like:

$n = (string)10000000000000000000001;

This doesn't work anymore.

for gmp gives this error:

gmp_mod(): Unable to convert variable to GMP - string is not an integer

And about bc, gives me this error:

bcmod(): Division by zero

The problem is, (string) doesn't convert it to string fine. Any idea?

Edit: I found a solution here, but still the input is string:

$bigint = gmp_init("9999999999999999999");
$bigint_string = gmp_strval($bigint);
var_dump($bigint_string);
Community
  • 1
  • 1
Vahid Najafi
  • 4,654
  • 11
  • 43
  • 88

1 Answers1

1

If You are taking an input for $n it would give you as a string not as int and if you have type casted as int at any point ... taking care the max size of int, the above given no is converted to 1.0E+22 now what happens when you try to type cast (string)1.0E+22 it becomes "1.0E+22" which is obviously just a string and can not be converted to gmp number.

So You need to convert scientific notation to string which will auto include , hence you also need to replace those commas

$n = 10000000000000000000001;
$n = str_replace(",", "", number_format($n));
M A SIDDIQUI
  • 2,028
  • 1
  • 19
  • 24