zeroclickinfo-goodies/lib/DDG/Goodie/PercentError.pm

27 lines
752 B
Perl
Raw Normal View History

2012-02-29 15:21:49 -08:00
package DDG::Goodie::PercentError;
use DDG::Goodie;
triggers start => "percent error", "% error", "%err", "%error", "percenterror", "percent err";
2012-02-29 15:21:49 -08:00
2012-03-20 21:08:12 -07:00
zci answer_type => "percent_error";
zci is_cached => 1;
2012-02-29 15:21:49 -08:00
handle remainder => sub {
my $length = length($_);
2012-05-23 20:34:11 -07:00
#return unless $length == 2;
2012-02-29 15:21:49 -08:00
my ( $acc, $exp ) = split /\s*[\s;,]\s*/, $_;
2012-03-22 12:22:12 -07:00
return unless $acc =~ /^-?\d+?(?:\.\d+|)$/ && $exp =~ /^-?\d+?(?:\.\d+|)$/;
2012-02-29 15:21:49 -08:00
my $diff = abs $acc - $exp;
2012-03-22 17:11:41 -07:00
my $per = abs ($diff/$acc);
my $err = $per*100;
2012-04-04 12:21:45 -07:00
my $html = qq(Accepted: $acc Experimental: $exp Error: <a href="javascript:;" onclick="document.x.q.value='$per';document.x.q.focus();">$err%</a>);
2012-02-29 15:21:49 -08:00
return "Accepted: $acc Experimental: $exp Error: $err%", html => $html;
2012-02-29 15:21:49 -08:00
};
1;