2012-02-29 15:21:49 -08:00
|
|
|
package DDG::Goodie::PercentError;
|
|
|
|
|
|
|
|
use DDG::Goodie;
|
|
|
|
|
2012-05-29 18:27:16 -07:00
|
|
|
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";
|
2012-03-20 19:23:09 -07:00
|
|
|
zci is_cached => 1;
|
2012-02-29 15:21:49 -08:00
|
|
|
|
2012-11-06 15:21:49 -08:00
|
|
|
primary_example_queries 'percent-error 34.5 35';
|
|
|
|
secondary_example_queries '%err 41 43', '%-error 2.88 2.82';
|
|
|
|
description 'find the percent error given accepted and experimental values';
|
|
|
|
name 'PercentError';
|
|
|
|
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/PercentError.pm';
|
|
|
|
category 'calculations';
|
|
|
|
topics 'math';
|
|
|
|
attribution twitter => 'crazedpsyc',
|
|
|
|
cpan => 'CRZEDPSYC' ;
|
|
|
|
|
2012-05-23 19:05:08 -07: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
|
|
|
|
2012-05-29 18:27:16 -07: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-03-20 19:23:09 -07:00
|
|
|
|
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
|
|
|
|
2012-03-20 19:23:09 -07:00
|
|
|
return "Accepted: $acc Experimental: $exp Error: $err%", html => $html;
|
2012-02-29 15:21:49 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
1;
|