-2

I have the next code in javascript:

var exponential = 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746;


var numerator, denominator;

function fact(x) {
if(x==0) {
    return 1;
}
return x * fact(x-1);
}



function poisson(k, landa) {
    exponentialPower = Math.pow(exponential, -landa); // negative power k
    landaPowerK = Math.pow(landa, k); // Landa elevated k
    numerator = exponentialPower * landaPowerK;
    denominator = fact(k); // factorial of k.

    return (numerator / denominator);
}

I need parse to php but i don't know how...

Can somebody help me?

krosgore
  • 3
  • 2
  • 1
    SO is not a code conversion service. However if you give it a go we will be more than willing to help you with any problems – RiggsFolly Dec 16 '18 at 17:48
  • 1
    Welcome, to improve your experience on SO please read [how to ask](https://stackoverflow.com/help/how-to-ask) an [On Topic question](https://stackoverflow.com/help/on-topic), and the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and [TAKE THE TOUR](http://stackoverflow.com/tour) – RiggsFolly Dec 16 '18 at 17:48
  • For what it's worth, JavaScript numbers do not have the precision to represent that value to the accuracy in your code; also you can just use `Math.E`. – Pointy Dec 16 '18 at 17:56
  • @RiggsFolly I've already tried it on my own but I do not get it. That's why I ask for help. – krosgore Dec 16 '18 at 18:18
  • @Pointy thanks ! I don't know about math.E – krosgore Dec 16 '18 at 18:20

1 Answers1

0

you should put below variables within poisson function also dont think you need to initialize $numerator and $dominator to 0;

$exponential = 2;
$numerator = 0; 
$dominator = 0; 

 function fact($x) {
        if($x==0) {
            return 1;
        }
        return $x * fact($x-1);
        }


        function poisson($k, $landa)
{
        $exponential = 2;
        $exponentialPower = pow($exponential, -$landa);
        $landaPowerK = pow($landa,$k);
        $numerator = $exponentialPower * $landaPowerK;

        $dominator = fact($k);

        echo ($numerator / $dominator);
}

poisson(1,2);
rs007
  • 405
  • 4
  • 8