1

Hello I would like to display in my code a full number to 20 decimal without scientific notation. I think it's a memory problem in php. can you help me ? thanks

function get($l, $c)
{
    $value = 0;

    if (0 <= $c && $c <= $l && $l < 5000) {
        $tab = [];

        for ($i = 0; $i <= $l; $i++) {

            for ($j = 0; $j <= $c; $j++) {

                if ($i == $j || $i - 1 <= 0 || $j <= 0) {
                    $tab[$i][$j] = 1;
                    $value = $tab[$i][$j];
                } elseif ($i != $j) {
                    $tab[$i][$j] = ($tab[$i - 1][$j - 1]) + ($tab[$i - 1][$j]);
                    $value = $tab[$i][$j];
                }
            }
        }
    }
    return $value;
}
echo get(67, 34); // found  :1.422652073762E+19 , excpected:14226520737620288370
tamda
  • 13
  • 5

2 Answers2

1

Have a look at BCMath functions:

function get($l, $c)
{
    $value = 0;

    if (0 <= $c && $c <= $l && $l < 5000) {
        $tab = [];

        for ($i = 0; $i <= $l; $i++) {

            for ($j = 0; $j <= $c; $j++) {

                if ($i == $j || $i - 1 <= 0 || $j <= 0) {
                    $tab[$i][$j] = 1;
                    $value = $tab[$i][$j];
                } elseif ($i != $j) {
                    // magic happens here
                    $tab[$i][$j] = bcadd($tab[$i - 1][$j - 1], $tab[$i - 1][$j]); 
                    $value = $tab[$i][$j];
                }
            }
        }
    }
    return $value;
}

$result = get(67, 34);
var_dump($result == '14226520737620288370');
echo $result;

Output

bool(true)
14226520737620288370

Working example.

SirPilan
  • 4,649
  • 2
  • 13
  • 26
0

There is that : https://www.php.net/manual/en/function.number-format.php

You can print any number and choose number of decimals you want

Victorbzh
  • 84
  • 1
  • 1
  • 6
  • I have already tried it does not work, it does not display the same thing there is a difference on the last 4 decimal places with : $sol = number_format($value, 0, '', ''); it display 14226520737620287488 – tamda Feb 14 '21 at 13:53
  • @user15140816 Did you try `echo number_format( get(67, 34),0); // = 14,226,520,737,620,287,488` – RiggsFolly Feb 14 '21 at 13:57
  • yes but the result excpected is : 14226520737620288370 – tamda Feb 14 '21 at 13:58
  • 1
    Are you completely sure about that – RiggsFolly Feb 14 '21 at 13:59
  • Yes sur I tried several functions exemple $large_number = 14226520737620288370; var_dump($large_number) ; it display : float(1.422652073762E+19) – tamda Feb 14 '21 at 14:02
  • Try this `$large_number = 14226520737620288370; var_dump($large_number); $large_number = 14226520737620287488; var_dump($large_number);` See they both give the same – RiggsFolly Feb 14 '21 at 14:12