简体   繁体   中英

PHP: issues with large value calculations with this algorithm

Hi I got this function in php running a mapping calculation. But it seems the output calculation value is completely incorrect (1633375984562987776)- expecting (1633375984562987887). Do I need some kind of special functions for these large math calculations? Any ideas on what i'm doing wrong?

linktomediaid("Baq6s3Qh-Nv");
function linktomediaid($url) {
        $code = $url;
        echo "    USING CODE - '$url' \n"; 
        $alphabet = [
                '-' => 62, '1' => 53, '0' => 52, '3' => 55, '2' => 54, '5' => 57, '4' => 56, '7' => 59, '6' => 58, '9' => 61,
                '8' => 60, 'A' => 0, 'C' => 2, 'B' => 1, 'E' => 4, 'D' => 3, 'G' => 6, 'F' => 5, 'I' => 8, 'H' => 7,
                'K' => 10, 'J' => 9, 'M' => 12, 'L' => 11, 'O' => 14, 'N' => 13, 'Q' => 16, 'P' => 15, 'S' => 18, 'R' => 17,
                'U' => 20, 'T' => 19, 'W' => 22, 'V' => 21, 'Y' => 24, 'X' => 23, 'Z' => 25, '_' => 63, 'a' => 26, 'c' => 28,
                'b' => 27, 'e' => 30, 'd' => 29, 'g' => 32, 'f' => 31, 'i' => 34, 'h' => 33, 'k' => 36, 'j' => 35, 'm' => 38,
                'l' => 37, 'o' => 40, 'n' => 39, 'q' => 42, 'p' => 41, 's' => 44, 'r' => 43, 'u' => 46, 't' => 45, 'w' => 48,
                'v' => 47, 'y' => 50, 'x' => 49, 'z' => 51
            ];
        $n = 0;
        for ($i = 0; $i < strlen($code); $i++) {
            $c = $code[$i];
        $n = $n * 64 + $alphabet[$c];
            echo "    n calculation value - $n\n";
        }
        $newvalue = sprintf ("%.0f", $n);
        echo "    final n value - $newvalue\n";
        return $n;
    }

This is the output i'm seeing in my terminal:

USING CODE - 'Baq6s3Qh-Nv'
    n calculation value - 1
    n calculation value - 90
    n calculation value - 5802
    n calculation value - 371386
    n calculation value - 23768748
    n calculation value - 1521199927
    n calculation value - 97356795344
    n calculation value - 6230834902049
    n calculation value - 3.987734337312E+14
    n calculation value - 2.5521499758797E+16
    n calculation value - 1.633375984563E+18
    final n value - 1633375984562987776

You are getting really close to the maximum value that an integer in PHP will store. You are probably experiencing an overflow. When you go over the limit in PHP, it will automatically switch to a floating point number:

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.

Floating point numbers a not perfectly precise, which is why they allow you such a large range. Your calculation probably exceed the max integer value, and thus it began using floating point arithmetic, so precision was lost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM