views:

157

answers:

4

hi there, 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

+2  A: 

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.

Matthew Flaschen
A: 

If it is getting recognized as a string you could try casting it to an int using

(int)$total[1];

To be honest, you could probably cast the $total array into an int right when you do the string split:

(int)$total = ...;

Strings that represent numbers can also be cast into (float), and depending on which version of php you have (double).

A: 

Couldn't reproduce this issue:

$total = 2222; // some imaginary number as I don't know your $odds and $evens;
$total = str_split($total);
var_dump($total);
/*
 *array(4) {
 *  [0]=>
 *  string(1) "2"
 *  [1]=>
 *  string(1) "2"
 *  [2]=>
 *  string(1) "2"
 *  [3]=>
 *  string(1) "2"
 *}
 */
var_dump($total[1]);
/*
 * string(1) "2"
 */
var_dump((10-($total[1])));
/*
 * int(8)
 */

Absolutely the expected behavior...

Stefan Gehrig
A: 

I added this as an answer because in a comment is not enough space:

If this is the implementation of the algorithm described here i really think that modulo check 6014359000000928 % 8 == 0 shouldn't be there.

For example consider the number with the first 15 digits like that: 6014 3590 0000 062. For that evens is 15, odds is 24, total is 39 and check is 1. Any number modulo 1 is 0. So 6014 3590 0000 0628 is valid as 6014 3590 0000 0620 is or 6014 3590 0000 0627. That doesn't make sense.

I think you have to check the last digit for equality with check. In that case only 6014 3590 0000 0621 would be valid.

rudi-moore