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;
}
?>
Because numeric types have a maximum limit, and if the calculation exceeds this limit, the result is "infinite"
– hkotsubo
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.
– user178974
(and add an Epsilon at the end to correct processor rounding).
– user178974