From a6573a7b2c8c2d7178a9414a84436533055c0764 Mon Sep 17 00:00:00 2001 From: Ben Moon Date: Tue, 8 Dec 2015 19:17:52 +0000 Subject: [PATCH] Fix issue #1694 Fixes issue with expressions such as `1 + 2e-7` treating `e` as the mathematical constant rather than `* 10 ^`. --- lib/DDG/Goodie/Calculator.pm | 6 +++--- t/Calculator.t | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/DDG/Goodie/Calculator.pm b/lib/DDG/Goodie/Calculator.pm index a1a8acfef..7e6899109 100644 --- a/lib/DDG/Goodie/Calculator.pm +++ b/lib/DDG/Goodie/Calculator.pm @@ -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*(? qr/>400,001 test_zci( + '(3 * 10 ^- 2) * 9 = 0.27', + heading => 'Calculator', + structured_answer => { + input => ['(3 * 10 ^- 2) * 9'], + operation => 'Calculate', + result => qr/>0.27 test_zci( + '(7 * 10 ^- 4) * 8 = 0.0056', + heading => 'Calculator', + structured_answer => { + input => ['(7 * 10 ^- 4) * 8'], + operation => 'Calculate', + result => qr/>0.0056 test_zci( 'pi / (1 * 10 ^ 9) = 3.14159265358979 * 10^-9', heading => 'Calculator',