Search code examples
phpmathoperatorsphantom-reference

$n = 2; 10-$n = 87


well this is what i am doing:

$total = (array_sum($odds))+$evens;
$total = str_split($total);
echo 'total[1]: '.$total[1].'<br />';
echo '10-$total[1]: ' . (10-($total[1]));

and the output is:

total[1]: 2
10-$total[1]: 87

my guess is it is being treated as a string, but how do i fix it?

so, what i want to know is
wh does (10-($total[1])); = 87?


Update:
yeah my mistake, a phantom 7,
but can anyone now tell me why:

echo $flybuys.' % '.$check.'<br />';
   $res = $flybuys % $check;
   echo 'res: '.$res;

outputs: 6014359000000928 % 8 res: 7


Solution

  • The inaccurate modulus result is because 6014359000000928 (~2^52) is beyond the bounds of an int, so PHP interprets it as a float. That implies you have a 32-bit system (PHP data type sizes vary depending on architecture). If you need to do math on large numbers, you can use a library like GMP. E.g.:

    $flybuys = gmp_init("6014359000000928");
    $res = gmp_mod($flybuys, 8);
    

    Make sure you pass large numbers to GMP as strings.