pbot/modules/math.pl

46 lines
884 B
Perl
Raw Normal View History

2007-05-20 22:44:44 +02:00
#!/usr/bin/perl -w
# Quick and dirty by :pragma
my ($arguments, $response, $invalid);
2007-05-20 22:44:44 +02:00
my @valid_keywords = (
'sin', 'cos', 'tan', 'atan', 'exp', 'int', 'hex', 'oct', 'log', 'sqrt',
'floor', 'ceil', 'asin', 'acos', 'log10', 'sinh', 'cosh', 'tanh', 'abs',
'pi'
);
2007-05-20 22:44:44 +02:00
if ($#ARGV < 0)
{
print "Dumbass.\n";
exit 0;
2007-05-20 22:44:44 +02:00
}
$arguments = join(' ', @ARGV);
2007-05-20 22:44:44 +02:00
if($arguments =~ m/([\$`\|])/) {
$invalid = $1;
} else {
while($arguments =~ /([a-zA-Z]+)/g) {
my $keyword = $1;
$invalid = $keyword and last if not grep { /^$keyword$/ } @valid_keywords;
}
}
if($invalid) {
print "Illegal symbol '$invalid' in equation\n";
exit 1;
}
$response = eval("use POSIX qw/ceil floor/; use Math::Complex;" . $arguments);
if($@) {
my $error = $@;
$error =~ s/ at \(eval \d+\) line \d+.//;
$error =~ s/[\n\r]+//g;
print $error;
exit 1;
2007-05-20 22:44:44 +02:00
}
print "$arguments = $response\n";