Fixes issue with expressions such as `1 + 2e-7` treating `e` as
the mathematical constant rather than `* 10 ^`.
master
Ben Moon 2015-12-08 19:17:52 +00:00
parent b4ebf96b60
commit a6573a7b2c
2 changed files with 21 additions and 3 deletions

View File

@ -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.
@ -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
@ -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×∙⋅\*\/÷\%]|(?<!e)\-|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);

View File

@ -705,6 +705,24 @@ 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</
}
),
'pi/1e9' => test_zci(
'pi / (1 * 10 ^ 9) = 3.14159265358979 * 10^-9',
heading => 'Calculator',