homepage Welcome to WebmasterWorld Guest from 23.22.128.96
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Divison by zero
medranoenrique




msg:3723888
 4:03 pm on Aug 15, 2008 (gmt 0)

I made a new CGI script that is simier to the one that i finished. The problem is that i had to "cheat" to get it to not send me an error message about dividing by zero.

#!/usr/local/bin/perl

use CGI;
$query = new CGI;

$var1 = ($query->param("var1"));
$lcseq = lc($var1);
chomp ($lcseq);
$oligolength = length($lcseq);
$SCRIPT_uri = $ENV{'SCRIPT_URI'};

$tm = 0;
$charpos = 0;
until($charpos == $oligolength)
{
$pair = substr($lcseq, $charpos, 2);
if ($pair eq "aa"){$tm += 2.116;}
if ($pair eq "ac"){$tm += 3.068;}
if ($pair eq "ag"){$tm += 2.750;}
if ($pair eq "at"){$tm += 1.862;}
if ($pair eq "ca"){$tm += 3.068;}
if ($pair eq "cc"){$tm += 3.893;}
if ($pair eq "cg"){$tm += 4.591;}
if ($pair eq "ct"){$tm += 2.708;}
if ($pair eq "ga"){$tm += 2.750;}
if ($pair eq "gc"){$tm += 4.739;}
if ($pair eq "gg"){$tm += 3.893;}
if ($pair eq "gt"){$tm += 3.047;}
if ($pair eq "ta"){$tm += 1.227;}
if ($pair eq "tc"){$tm += 2.708;}
if ($pair eq "tg"){$tm += 3.047;}
if ($pair eq "tt"){$tm += 2.116;}
$charpos = $charpos +1;
}

###################################### start %gc

$at = 0;
$gc = 0;
$gcpercent = 0;
$charpos = 0;
until($charpos == $oligolength)
{
$base = substr($lcseq, $charpos, 1);
if ($base eq "a") {$at += 1}
if ($base eq "c") {$gc += 1}
if ($base eq "g") {$gc += 1}
if ($base eq "t") {$at += 1}
$charpos = $charpos +1;
}

$gcpercent = ($gc / (($at + $gc) +
0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 )); ####cheated part
###################################### end %gc

$tm = $tm + (5*$gcpercent);


print "Content-type: text/html\n\n";

print "<html>";
print "<head><title>tm calculator</title></head>";
print "<body>";

if ($var1 eq '') {
print "<form action=\"$SCRIPT_uri\" method=\"POST\">";
print "Sequence: <input type=\"text\" name=\"var1\">\n";
print "<input type=\"submit\" value=\"submit\">\n";
print "</form>";

} else {
print "Your sequence is $oligolength characters.\n";
print "The tm of your sequence is $tm";
}

print "</body>";
print "</html>";

exit;

 

Dabrowski




msg:3723989
 6:22 pm on Aug 15, 2008 (gmt 0)

And your question?

janharders




msg:3724057
 8:00 pm on Aug 15, 2008 (gmt 0)

the problem is in this line:

$gcpercent = ($gc / (($at + $gc) +
0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 )); ####cheated part

(since that's the only division I could see)

timster




msg:3724089
 8:46 pm on Aug 15, 2008 (gmt 0)

Yes, that's a dirty cheat. Generally you want to handle div by zero explicitly. You could get a little fancier and use an eval block, but something like this would work fine here:

my $div_by_zero; # if needed
if ($at + $gc) {
$gcpercent = ($gc / ($at + $gc));
$tm = $tm + (5*$gcpercent);

} else {

# Handle the division by zero, however it is appropriate
$tm = "Undefined."; # Maybe?
$div_by_zero = 1; # If you need this
}

I'm guessing this app is for building oligonucleotides. That's pretty technical stuff; it's hard to tell what your app should be doing.

LifeinAsia




msg:3724158
 10:32 pm on Aug 15, 2008 (gmt 0)

Generally, when you have a divide by zero, it's because you have bad data or a situation you shouldn't be in in the first place. So rather than returning an incorrect fudged value, it's usually better practice to return an exception to allow the user to re-enter data or fix the problem. But as timster says, it's kind of hard to tell what the app should be doing, so difficult to advise what the correct answer should be.

Um, exactly what was the question again?

RudyS




msg:3724520
 4:43 pm on Aug 16, 2008 (gmt 0)

what Enrique didn't include (I think) was that the "Internal Server Error" page comes up as soon as the link is clicked ... with the "dirty cheat" added the submit page opens fine so it looks like the CGI has some strict rules that have to be taken into account? the error message in the error log says "division by zero" ... so it seems that the script is "evaluated?" for a possible division by zero before there is a chance to submit a string for the input variable?

sounds like it is standard practice to be on the lookout for all possible divisions by zero? for example, any time a division operation includes a variable in the denominator? I'm going to include timster's "explicit" handling of the gcpercent assignment ... and i guess always include timster's explicit handling of division?

Enrique finished his internship on Friday and left full of gratitude ... he will be a High School Sophmore in the Fall ! ... I'm sure he wont soon forget you guys and all you taught him ...

thanks again and again

rudyS

perl_diver




msg:3724593
 7:48 pm on Aug 16, 2008 (gmt 0)

The 500 error comes up because the script returns a fatal error before an http header has been printed. Print the http header earlier in the script, or use the CGI::Carp module, which was written specifically for the purpose of capturing fatals errors (and warnings) so they don't crash a CGI script but die gracefully. I'm sure this has been mentioned previously.

lexipixel




msg:3728818
 1:50 pm on Aug 22, 2008 (gmt 0)

Maybe you can invert the equation and multiply instead of dividing.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved