]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Math.pl
Initial revision
[infobot.git] / src / Modules / Math.pl
1 #
2 # infobot copyright (C) kevin lenzo 1997-98
3 #
4
5 if (&IsParam("useStrict")) { use strict; }
6
7 my %digits = (
8         "first",   "1",
9         "second",  "2",
10         "third",   "3",
11         "fourth",  "4",
12         "fifth",   "5",
13         "sixth",   "6",
14         "seventh", "7",
15         "eighth",  "8",
16         "ninth",   "9",
17         "tenth",   "10",
18         "one",     "1",
19         "two",     "2",
20         "three",   "3",
21         "four",    "4",
22         "five",    "5",
23         "six",     "6",
24         "seven",   "7",
25         "eight",   "8",
26         "nine",    "9",
27         "ten",     "10"
28 );
29
30 sub perlMath {
31     my($locMsg) = $message;
32
33     if ($message =~ /^\s*$/) {
34         return;
35     }
36
37     foreach (keys %digits) {
38         $locMsg =~ s/$_/$digits{$_}/g;
39     }
40
41     while ($locMsg =~ /(exp ([\w\d]+))/) {
42         my($exp, $val) = ($1, exp $2);
43         $locMsg =~ s/$exp/+$val/g;
44     }
45
46     while ($locMsg =~ /(hex2dec\s*([0-9A-Fa-f]+))/) {
47         my($exp, $val) = ($1, hex $2);
48         $locMsg =~ s/$exp/+$val/g;
49     }
50
51     if ($locMsg =~ /^\s*(dec2hex\s*(\d+))\s*\?*/) {
52         my ($exp, $val) = ($1, sprintf("%x", "$2"));
53         $locMsg =~ s/$exp/+$val/g;
54     }
55
56     my $e = exp(1);
57     $locMsg =~ s/\be\b/$e/;
58
59     while ($locMsg =~ /(log\s*((\d+\.?\d*)|\d*\.?\d+))\s*/) {
60         my ($exp, $res) = ($1, $2);
61         my $val = ($res) ? log($res) : "Infinity";
62         $locMsg =~ s/$exp/+$val/g;
63     }
64
65     while ($locMsg =~ /(bin2dec ([01]+))/) {
66         my $exp = $1;
67         my $val = join ("", unpack("B*",$2)) ;
68         $locMsg =~ s/$exp/+$val/g;
69     }
70
71     while ($locMsg =~ /(dec2bin (\d+))/) {
72         my $exp = $1;
73         my $val = join('', unpack('B*', pack('N',$2)));
74         $val =~ s/^0+//;
75         $locMsg =~ s/$exp/+$val/g;
76     }
77
78     for ($locMsg) {
79         s/\bpi\b/3.1415/g;
80         s/ to the / ** /g;
81         s/\btimes\b/\*/g;
82         s/\bdiv(ided by)? /\/ /g;
83         s/\bover /\/ /g;
84         s/\bsquared/\*\*2 /g;
85         s/\bcubed/\*\*3 /g;
86         s/\bto\s+(\d+)(r?st|nd|rd|th)?( power)?/\*\*$1 /ig;
87         s/\bpercent of/*0.01*/ig;
88         s/\bpercent/*0.01/ig;
89         s/\% of\b/*0.01*/g;
90         s/\%/*0.01/g;
91         s/\bsquare root of (\d+)/$1 ** 0.5 /ig;
92         s/\bcubed? root of (\d+)/$1 **(1.0\/3.0) /ig;
93         s/ of / * /;
94         s/(bit(-| )?)?xor(\'?e?d( with))?/\^/g;
95         s/(bit(-| )?)?or(\'?e?d( with))?/\|/g;
96         s/bit(-| )?and(\'?e?d( with))?/\& /g;
97         s/(plus|and)/+/ig;
98     }
99
100     # what the hell is this shit?
101     if (($locMsg =~ /^\s*[-\d*+\s()\/^\.\|\&\*\!]+\s*$/)
102         && ($locMsg !~ /^\s*\(?\d+\.?\d*\)?\s*$/)
103         && ($locMsg !~ /^\s*$/)
104         && ($locMsg !~ /^\s*[( )]+\s*$/))
105     {
106         $locMsg = eval($locMsg);
107
108         if (defined $locMsg and $locMsg =~ /^[-+\de\.]+$/) {
109             $locMsg = sprintf("%1.12f", $locMsg);
110             $locMsg =~ s/\.?0+$//;
111
112             if (length($locMsg) > 30) {
113                 $locMsg = "a number with quite a few digits...";
114             }
115         } else {
116             if (defined $locMsg) {
117                 &DEBUG("math: locMsg => '$locMsg'... FIXME");
118             } else {
119                 $locMsg = "undefined";
120             }
121         }
122     } else {
123         $locMsg = "";
124     }
125
126     if ($locMsg ne $message) {
127         return $locMsg;
128     } else {
129         return '';
130     }
131 }
132
133 1;