Skip to content

Instantly share code, notes, and snippets.

@eqhmcow
Created January 3, 2012 19:09
Show Gist options
  • Save eqhmcow/1556389 to your computer and use it in GitHub Desktop.
Save eqhmcow/1556389 to your computer and use it in GitHub Desktop.
safe divide by zero
#!/usr/bin/perl
sub divide {
($a, $b) = @_;
$c = eval { $a / $b };
$@ =~ m/Illegal division by zero/ && rand() > .5 ? ($b = 1) : ($b = -1);
$@ && ($c = $a / $b);
return $c;
}
print divide(1,4), "\n";
print divide(1,0), "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment