Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1802 from Mailkov/issue1418
Browse files Browse the repository at this point in the history
Issue #1418 - Factorial: Move integration into Calculator.pm
  • Loading branch information
moollaza committed Nov 25, 2015
2 parents 28cc59b + 75a9d44 commit 897a617
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 97 deletions.
8 changes: 7 additions & 1 deletion lib/DDG/Goodie/Calculator.pm
Expand Up @@ -7,6 +7,7 @@ with 'DDG::GoodieRole::NumberStyler';

use List::Util qw( max );
use Math::Trig;
use Math::BigInt;
use utf8;

zci answer_type => "calc";
Expand All @@ -33,7 +34,7 @@ triggers query_nowhitespace => qr<
[\( \) x X × ∙ ⋅ * % + ÷ / \^ 0-9 \. , _ \$ -]*
(?(1) (?: -? [0-9 \. , _ ]+ |) |)
(?: [\( \) x X × ∙ ⋅ * % + ÷ / \^ \$ -] | times | divided by | plus | minus | cos | sin | tan | cotan | log | ln | log[_]?\d{1,3} | exp | tanh | sec | csc | squared | sqrt | pi | e )+
(?: [\( \) x X × ∙ ⋅ * % + ÷ / \^ \$ -] | times | divided by | plus | minus | fact | factorial | cos | sin | tan | cotan | log | ln | log[_]?\d{1,3} | exp | tanh | sec | csc | squared | sqrt | pi | e )+
(?: [0-9 \. ,]* )
(?: gross | dozen | pi | e | c | squared | score |)
Expand All @@ -59,6 +60,7 @@ my %named_operations = (
'÷' => '/',
'ln' => 'log', # perl log() is natural log.
'squared' => '**2',
'fact' => 'Math::BigInt->new',
);

my %named_constants = (
Expand All @@ -85,9 +87,11 @@ handle query_nowhitespace => sub {
return if ($query =~ /^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/); # Probably are searching for a phone number, not making a calculation

$query =~ s/^(?:whatis|calculate|solve|math)//;
$query =~ s/(factorial)/fact/; #replace factorial with fact

# 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 @@ -114,6 +118,7 @@ handle query_nowhitespace => sub {
return unless $style;

$tmp_expr = $style->for_computation($tmp_expr);
$tmp_expr =~ s/(Math::BigInt->new\((.*)\))/(Math::BigInt->new\($2\))->bfac()/g; #correct expression for fact
# Using functions makes us want answers with more precision than our inputs indicate.
my $precision = ($query =~ $funcy) ? undef : ($query =~ /^\$/) ? 2 : max(map { $style->precision_of($_) } @numbers);
my $tmp_result;
Expand Down Expand Up @@ -152,6 +157,7 @@ sub prepare_for_display {
# Show them how 'E' was interpreted. This should use the number styler, too.
$query =~ s/((?:\d+?|\s))E(-?\d+)/\($1 * 10^$2\)/i;
$query =~ s/\s*\*{2}\s*/^/g; # Use prettier exponentiation.
$query =~ s/(Math::BigInt->new\((.*)\))/fact\($2\)/g; #replace Math::BigInt->new( with fact(
$result = $style->for_display($result);
foreach my $name (keys %named_constants) {
$query =~ s#\($name\)#$name#xig;
Expand Down
44 changes: 0 additions & 44 deletions lib/DDG/Goodie/Factorial.pm

This file was deleted.

18 changes: 18 additions & 0 deletions t/Calculator.t
Expand Up @@ -759,6 +759,24 @@ ddg_goodie_test(
result => qr"6.28318530717958"
}
),
'fact(3)' => test_zci(
'fact(3) = 6',
heading => 'Calculator',
structured_answer => {
input => ['fact(3)'],
operation => 'Calculate',
result => qr/>6</
}
),
'factorial(3)' => test_zci(
'fact(3) = 6',
heading => 'Calculator',
structured_answer => {
input => ['fact(3)'],
operation => 'Calculate',
result => qr/>6</
}
),
'123.123.123.123/255.255.255.255' => undef,
'83.166.167.160/27' => undef,
'9 + 0 x 07' => undef,
Expand Down
52 changes: 0 additions & 52 deletions t/Factorial.t

This file was deleted.

0 comments on commit 897a617

Please sign in to comment.