开发者

$n = 2; 10-$n = 87

开发者 https://www.devze.com 2023-01-07 18:59 出处:网络
well this is what i am doing: $total = (array_sum($odds))+$evens; $total = str_split($total); echo \'total[1]: \'.$total[1].\'<br />\';

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 o开发者_开发知识库utput 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


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.


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).


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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消