Why does factorial above 170 return infinite?

Asked

Viewed 298 times

4

I made this php code to calculate the factor of positive integer numbers , however , for numbers from 170 the result is always infinite and I would like to understand why this.

Ex3

 $numero_usuario = isset($_POST['a'])?$_POST['a']:0;
 $fatorial = $numero_usuario;

 if($numero_usuario == 0){
   echo $numero_usuario;
 }
 if($numero_usuario > 0){
   while($fatorial!=1){
       $fatorial--;
       $numero_usuario *= $fatorial;

   }
   echo $numero_usuario;
 }
 ?>

  • 2

    Because numeric types have a maximum limit, and if the calculation exceeds this limit, the result is "infinite"

  • 171! = 1.2 10 309 > 1.8e308 (the maximum double value) (https://www.wolframalpha.com/). If you need to calculate large factorials, you will need floating point number with arbitrary precision. But if you are calculating arrangements and combinations, calculate the quotients first.

  • (and add an Epsilon at the end to correct processor rounding).

2 answers

6


In the Soen you can find an answer on that, which I adapted and added some things I found researching:

What happens is that you are exceeding the precision of a double in a 64bit system. This precision varies between 10^−308 and 10^308. In approximate values:

  • Factor of 170: 7.25741562 * 10^307 (within the limit of accuracy)
  • Factor of 171: 1.24101807 * 10^309 (outside the limit of precision, then "infinite")

Then it is recommended to use an extension capable of doing this kind of multiplication. One of the possibilities is the Bcmath (Bcmath Arbitrary Precision Mathematics), which was the one I found the staff using for multiplication of large values (with the function bcmul).

Adapting your code:

while($fatorial!=1){
    $fatorial--;
    $numero_usuario = bcmul($numero_usuario, $fatorial);
}

Interesting fact: search by factor of 170 in Google and then by the factor of 171:

O Google calcula o Fatorial de 170 porém exibe Undefined para 171!

-1

The factorial with a low value already returns a very high value, see

10! = 3.628.800

170 is exceeding the allocation memory limit stipulated by the Programming Language.

Take the example of Language C: en.wikibooks.org/wiki/Program_em_c/Type_de_data

The other languages follow the same way, but each with its specific allocation size.

  • 2

    170! Does not exceed the memory limit of the machine, does not tickle or tickle (short for Osca, syncopated form of tickle).

  • It may even be that the RAM memory does not, but exceeds the space of the size that the Programming Language uses to allocate the variable in memory. Each variable type has a maximum size supported. See an example of the language C: https://pt.wikibooks.org/wiki/Programar_em_C/Tipos_de_dados However, all languages have their limitations.

  • 2

    This is what has to be explained in the answer. The way he wrote implies that the factorial consumed all the RAM.

  • Yes, I expressed myself wrong. Thank you for the comment Augusto Vasques. Corrected information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.