]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Modules/Math.pl
* Add vim formatting comments ( # vim:ts=4:sw=4:expandtab:tw=80 )
[infobot.git] / src / Modules / Math.pl
index b7ba1568712a27c119f13344fe66d383aeed0a19..a3b966b247ceadc88b3d29393aa269ba277a665c 100644 (file)
@@ -2,29 +2,31 @@
 # infobot copyright (C) kevin lenzo 1997-98
 #
 
-if (&IsParam("useStrict")) { use strict; }
+use strict;
+
+use vars qw($message);
 
 my %digits = (
-       "first",   "1",
-       "second",  "2",
-       "third",   "3",
-       "fourth",  "4",
-       "fifth",   "5",
-       "sixth",   "6",
-       "seventh", "7",
-       "eighth",  "8",
-       "ninth",   "9",
-       "tenth",   "10",
-       "one",     "1",
-       "two",     "2",
-       "three",   "3",
-       "four",    "4",
-       "five",    "5",
-       "six",     "6",
-       "seven",   "7",
-       "eight",   "8",
-       "nine",    "9",
-       "ten",     "10"
+       'first',   '1',
+       'second',  '2',
+       'third',   '3',
+       'fourth',  '4',
+       'fifth',   '5',
+       'sixth',   '6',
+       'seventh', '7',
+       'eighth',  '8',
+       'ninth',   '9',
+       'tenth',   '10',
+       'one',     '1',
+       'two',     '2',
+       'three',   '3',
+       'four',    '4',
+       'five',    '5',
+       'six',     '6',
+       'seven',   '7',
+       'eight',   '8',
+       'nine',    '9',
+       'ten',     '10'
 );
 
 sub perlMath {
@@ -58,13 +60,13 @@ sub perlMath {
 
     while ($locMsg =~ /(log\s*((\d+\.?\d*)|\d*\.?\d+))\s*/) {
        my ($exp, $res) = ($1, $2);
-       my $val = ($res) ? log($res) : "Infinity";
+       my $val = ($res) ? log($res) : 'Infinity';
        $locMsg =~ s/$exp/+$val/g;
     }
 
     while ($locMsg =~ /(bin2dec ([01]+))/) {
        my $exp = $1;
-       my $val = join ("", unpack("B*",$2)) ;
+       my $val = join ('', unpack('B*',$2)) ;
        $locMsg =~ s/$exp/+$val/g;
     }
 
@@ -76,7 +78,7 @@ sub perlMath {
     }
 
     for ($locMsg) {
-       s/\bpi\b/3.1415/g;
+       s/\bpi\b/3.14159265/g;
        s/ to the / ** /g;
        s/\btimes\b/\*/g;
        s/\bdiv(ided by)? /\/ /g;
@@ -101,33 +103,40 @@ sub perlMath {
     if (($locMsg =~ /^\s*[-\d*+\s()\/^\.\|\&\*\!]+\s*$/)
        && ($locMsg !~ /^\s*\(?\d+\.?\d*\)?\s*$/)
        && ($locMsg !~ /^\s*$/)
-       && ($locMsg !~ /^\s*[( )]+\s*$/))
-    {
+       && ($locMsg !~ /^\s*[( )]+\s*$/)
+       && ($locMsg =~ /\d+/)
+    ) {
+       $locMsg =~ s/([0-9]+\.[0-9]+(\.[0-9]+)+)/"$1"/g;
        $locMsg = eval($locMsg);
 
        if (defined $locMsg and $locMsg =~ /^[-+\de\.]+$/) {
            $locMsg = sprintf("%1.12f", $locMsg);
            $locMsg =~ s/\.?0+$//;
 
-           if (length($locMsg) > 30) {
+           if (length $locMsg > 30) {
                $locMsg = "a number with quite a few digits...";
            }
        } else {
            if (defined $locMsg) {
-               &DEBUG("math: locMsg => '$locMsg'... FIXME");
+               &FIXME("math: locMsg => '$locMsg'...");
            } else {
-               $locMsg = "undefined";
+               &status("math: could not really compute.");
+               $locMsg = '';
            }
        }
     } else {
-       $locMsg = "";
+       $locMsg = '';
     }
 
-    if ($locMsg ne $message) {
+    if (defined $locMsg and $locMsg ne $message) {
+       # success.
        return $locMsg;
     } else {
+       # no match.
        return '';
     }
 }
 
 1;
+
+# vim:ts=4:sw=4:expandtab:tw=80