Skip to content

Commit

Permalink
Merge pull request duckduckgo#1866 from GuiltyDolphin/fix-exponent
Browse files Browse the repository at this point in the history
Calculator: Fix issue duckduckgo#1694
  • Loading branch information
mintsoft committed Dec 9, 2015
2 parents 843de93 + 204de9c commit fb1073e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/DDG/Goodie/Calculator.pm
Expand Up @@ -91,7 +91,7 @@ handle query_nowhitespace => sub {

# Grab expression.
my $tmp_expr = spacing($query, 1);


return if $tmp_expr eq $query; # If it didn't get spaced out, there are no operations to be done.

Expand All @@ -102,7 +102,7 @@ handle query_nowhitespace => sub {
}

$tmp_expr =~ s#log[_]?(\d{1,3})#(1/log($1))*log#xg; # Arbitrary base logs.
$tmp_expr =~ s/ (\d+?)E(-?\d+)([^\d]|\b) /\($1 * 10**$2\)$3/xg; # E == *10^n
$tmp_expr =~ s/ (\d+?)E(-?\d+)([^\d]|\b) /\($1 * 10**$2\)$3/ixg; # E == *10^n
$tmp_expr =~ s/\$//g; # Remove $s.
$tmp_expr =~ s/=$//; # Drop =.
$tmp_expr =~ s/([0-9])\s*([a-zA-Z])([^0-9])/$1*$2$3/g; # Support 0.5e or 0.5pi; but don't break 1e8
Expand Down Expand Up @@ -179,7 +179,7 @@ sub spacing {
my ($text, $space_for_parse) = @_;

$text =~ s/\s{2,}/ /g;
$text =~ s/(\s*(?<!<)(?:[\+\-\^xX×∙⋅\*\/÷\%]|times|plus|minus|dividedby)+\s*)/ $1 /ig;
$text =~ s/(\s*(?<!<)(?:[\+\^xX×∙⋅\*\/÷\%]|(?<!\de)\-|times|plus|minus|dividedby)+\s*)/ $1 /ig;
$text =~ s/\s*dividedby\s*/ divided by /ig;
$text =~ s/(\d+?)((?:dozen|pi|gross|squared|score))/$1 $2/ig;
$text =~ s/([\(\)])/ $1 /g if ($space_for_parse);
Expand Down
54 changes: 54 additions & 0 deletions t/Calculator.t
Expand Up @@ -705,6 +705,60 @@ ddg_goodie_test(
result => qr/>400,001</
}
),
'3e-2* 9 ' => test_zci(
'(3 * 10 ^- 2) * 9 = 0.27',
heading => 'Calculator',
structured_answer => {
input => ['(3 * 10 ^- 2) * 9'],
operation => 'Calculate',
result => qr/>0.27</
}
),
'7e-4 *8' => test_zci(
'(7 * 10 ^- 4) * 8 = 0.0056',
heading => 'Calculator',
structured_answer => {
input => ['(7 * 10 ^- 4) * 8'],
operation => 'Calculate',
result => qr/>0.0056</
}
),
'6 * 2e-11' => test_zci(
'6 * (2 * 10 ^- 11) = 1.2 * 10^-10',
heading => 'Calculator',
structured_answer => {
input => ['6 * (2 * 10 ^- 11)'],
operation => 'Calculate',
result => qr/>1\.2 \* 10<sup>-10<\/sup></
}
),
'7 + 7e-7' => test_zci(
'7 + (7 * 10 ^- 7) = 7.0000007',
heading => 'Calculator',
structured_answer => {
input => ['7 + (7 * 10 ^- 7)'],
operation => 'Calculate',
result => qr/>7.0000007</
}
),
'1 * 7 + e-7' => test_zci(
'1 * 7 + e - 7 = 2.71828182845905',
heading => 'Calculator',
structured_answer => {
input => ['1 * 7 + e - 7'],
operation => 'Calculate',
result => qr/>2.71828182845905</
}
),
'7 * e- 5' => test_zci(
'7 * e - 5 = 14.0279727992134',
heading => 'Calculator',
structured_answer => {
input => ['7 * e - 5'],
operation => 'Calculate',
result => qr/>14.0279727992134</
}
),
'pi/1e9' => test_zci(
'pi / (1 * 10 ^ 9) = 3.14159265358979 * 10^-9',
heading => 'Calculator',
Expand Down

0 comments on commit fb1073e

Please sign in to comment.