代码之家  ›  专栏  ›  技术社区  ›  Hailwood

$N=2;10-$N=87

  •  -1
  • Hailwood  · 技术社区  · 14 年前

    这就是我要做的:

    $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?


    更新:
    是的,我的错,幻影7,
    但现在有人能告诉我为什么:
    echo $flybuys.' % '.$check.'<br />';
       $res = $flybuys % $check;
       echo 'res: '.$res;
    

    输出: 60143590000000928%8 RES:7

    4 回复  |  直到 14 年前
        1
  •  2
  •   Matthew Flaschen    14 年前

    不准确的模数结果是因为60143590000000928(~2^52)超出了int的界限,所以php将其解释为浮点。这意味着您有一个32位系统(PHP数据类型大小因体系结构而异)。如果你需要对大数字进行数学运算,你可以使用类似的库 GMP . 例如。:

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

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

        2
  •  0
  •   vince88    14 年前

    如果它被识别为字符串,可以尝试使用

    (int)$total[1];
    

    老实说,在执行字符串拆分时,您可能会将$total数组强制转换为int:

    (int)$total = ...;
    

    表示数字的字符串也可以转换成(float),这取决于您使用的PHP版本(double)。

        3
  •  0
  •   Stefan Gehrig    14 年前

    无法复制此问题:

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

    当然是预期的行为…

        4
  •  0
  •   Community Egal    7 年前

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

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

    例如,考虑前15位数字如下: 6014 3590 0000 062 . 为此 evens 是15, odds 是24, total 是39和 check 是1。任何数模1都是0。所以 6014 3590 0000 0628 有效为 6014 3590 0000 0620 是或 6014 3590 0000 0627 . 这没有道理。

    我想你必须核对最后一个数字是否与 检查 . 只有在这种情况下 6014 3590 0000 0621 是有效的。