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