Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about Perl "exp" function? Please help!

Status
Not open for further replies.
Oct 11, 2006
5
US
In my Perl code I aim to compute the Gaussian integral, and the Gaussian function is expressed as

###code###
sub func
{
my $x = shift;
return (1/(sqrt($Vz*2*3.1416)+0.000001))*exp(-(($x-$Mz)**2)/((2*($Vz**2))+0.000001));
}
###code###

, where I integral from 0 to 1, and $Vz is the variance and $Mz is the mean. 0.000001 is used to avoid the illegal division by zero. However my script will just stop at some point during execution and not proceed anymore. I tried to debug, and figured out it "seemed" to be the problem of the part "exp(...)", because if I replaced the exp() with an arbitrary constant, say 1.5, the script would just work well. However I just didn't get what's wrong with the exp(...) code. It seemed pretty correct to me. Could anyone please help me on this? Thank you very much!!!
 
Sorry I should've followed the "code" rules. Here is my code - thanks!!!

Code:
sub func
{
    my $x = shift;
    return (1/(sqrt($Vz*2*3.1416)+0.000001))*exp(-(($x-$Mz)**2)/((2*($Vz**2))+0.000001));
}
 
What are some valid values for $x, $Vz and $Mz? Also, with your given values, what is the expected output of func()?

I plugged in some arbitrary values to the function you provided and it spit out an answer. I don't know if the output is right, but it's working.
 
Your code appears to be fine. I cannot imagine what might be leading exp to cause any problems, so I think it's probably something else. The only thing that I would suggest that you do is to treat define by 0 a little more explicitly, instead of introducing deviation from your expected value.

Code:
sub func
{
    my $x = shift;
    return 0 if $Vz == 0; # Avoid divide by Zero. (could also just die)
    return (1/(sqrt($Vz*2*3.1416))) * exp( -(($x-$Mz)**2)/(2*($Vz**2)) );
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top