$n = 2; 10-$n = 87

发布于 2024-09-10 04:08:54 字数 642 浏览 5 评论 0原文

好吧,这就是我正在做的:

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

输出是:

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

我的猜测是它被视为字符串,但我该如何修复它?

所以,我想知道的是
wh 做什么 (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;

输出: 6014359000000928 % 8 分辨率:7

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过去的过去 2024-09-17 04:08:54

模数结果不准确是因为 6014359000000928 (~2^52) 超出了 int 的范围,因此 PHP 将其解释为浮点数。这意味着您拥有 32 位系统(PHP 数据类型大小因体系结构而异)。如果您需要对大数进行数学运算,可以使用 GMP。例如:

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

确保将大量数字作为字符串传递给 GMP。

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.

简单气质女生网名 2024-09-17 04:08:54

如果它被识别为字符串,您可以尝试使用将其转换为 int

(int)$total[1];

说实话,您可以在进行字符串拆分时将 $total 数组转换为 int:

(int)$total = ...;

表示数字的字符串也可以转换为 ( float),并取决于您拥有的 php 版本(双精度)。

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

他夏了夏天 2024-09-17 04:08:54

无法重现此问题:

$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)
 */

绝对是预期的行为......

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

ㄟ。诗瑗 2024-09-17 04:08:54

我添加这个作为答案是因为评论中没有足够的空间:

如果这是描述的算法的实现 这里我真的认为模检查6014359000000928 % 8 == 0不应该在那里。

例如,考虑前 15 位数字,如下所示:6014 3590 0000 062。对于偶数为15,奇数为24,为39,检查为1。任何数字模1是 0。因此,6014 3590 0000 0628 的有效值为 6014 3590 0000 06206014 3590 0000 0627。这没有道理。

我认为您必须使用 check 检查最后一位数字是否相等。在这种情况下,只有 6014 3590 0000 0621 有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文