0

From my experience, php code and javascript one can be very portable, but today I found this:

$secret1 = 8992483;
$secret2 = 1785665;
$counter = 3288985389;
for ($i=0; $i<10000000; $i++) {
    $counter = ($counter * $secret1) % $secret2;
}
console.log($counter);

when executed in chrome/nodejs i get 652751, but in other languages like PHP or even C it should be: 1281709

What am I doing wrong? :S

Thanks

Edit: To avoid reaching max int you can use big-integer

var bigInt = require("big-integer");
$secret1 = 8992483;
$secret2 = 1785665;
$counter = bigInt(3288985389);
for ($i=0; $i<10000000; $i++) {
    $counter = $counter.multiply($secret1).mod($secret2);
}
console.log($counter);
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104

1 Answers1

2

It depends on how the language handles integer and integer overflows

For example, in certain languages, when you have the max_int + 1 == min_int

That is because the max integer is coded this way : 011111111...

Therefore when you add 1 you get : 100000000... which is the min integer

Alex
  • 1,368
  • 10
  • 20