3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-01 17:16:39 +02:00

Improve math.pl to support specific math functions (e.g., sqrt, etc)

This commit is contained in:
Pragmatic Software 2014-03-24 02:25:37 +00:00
parent ee3fddf1eb
commit 2a933bff87
2 changed files with 33 additions and 11 deletions

View File

@ -13,8 +13,8 @@ use warnings;
# These are set automatically by the build/commit script # These are set automatically by the build/commit script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 531, BUILD_REVISION => 532,
BUILD_DATE => "2014-03-22", BUILD_DATE => "2014-03-23",
}; };
1; 1;

View File

@ -2,22 +2,44 @@
# Quick and dirty by :pragma # Quick and dirty by :pragma
my ($arguments, $response, $invalid);
my ($arguments, $response); my @valid_keywords = (
'sin', 'cos', 'tan', 'atan', 'exp', 'int', 'hex', 'oct', 'log', 'sqrt',
'floor', 'ceil', 'asin', 'acos', 'log10', 'sinh', 'cosh', 'tanh', 'abs',
'pi'
);
if ($#ARGV < 0) if ($#ARGV < 0)
{ {
print "Dumbass.\n"; print "Dumbass.\n";
die; exit 0;
} }
$arguments = join(" ", @ARGV); $arguments = join(' ', @ARGV);
if($arguments =~ m/[\$a-z]/i) if($arguments =~ m/([\$`\|])/) {
{ $invalid = $1;
print("Illegal characters, please only use numbers and valid operators (+, -, /, *, etc)."); } else {
die(); while($arguments =~ /([a-zA-Z]+)/g) {
my $keyword = $1;
$invalid = $keyword and last if not grep { /^$keyword$/ } @valid_keywords;
}
} }
$response = eval($arguments); if($invalid) {
print "$arguments = $response"; 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;
}
print "$arguments = $response\n";