forked from dbb/scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperlcalc.pl
70 lines (59 loc) · 1.08 KB
/
perlcalc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl -wl
use POSIX qw(ceil floor);
use strict;
my $pi = 3.14159265358979;
my $e = 2.71828182845905;
foreach (@ARGV) {
s:\^:\*\*:g;
if (/system/ or /exec/) {
print "Found 'system'/'exec'; not evaluating.";
}
elsif (/[-]{1,2}[h]/ or /help/) {
&help;
}
elsif (/\$/) {
if (/\$e/ or /\$pi/) {
print eval;
}
}
elsif (defined) {
print $_ . ' = ' . eval;
warn $@ if $@;
}
else {
print "undefined argument";
}
}
sub help {
print "
constants: \$e, \$pi
format: binary (leading 0b), decimal,
hexadecimal (leading 0x), octal (leading 0),
scientific (e.g. 2.04e4),
operations: +, -, *, /, ^ or **, %, ()
ceil(), int(), floor()
log(); lg(base, x)
sin(), cos(), tan()
sind(), cosd(), tand()
";
}
sub sind {
my $x = sin(($_[0]/180) * $pi);
return $x;
}
sub cosd {
my $x = cos(($_[0]/180) * $pi);
return $x;
}
sub tan {
my $x = (sin($_[0]) / cos($_[0]));
return $x;
}
sub tand {
my $x = &tan(($_[0]/180) * $pi);
return $x;
}
sub lg {
my $y = log($_[1]) / log($_[0]);
return $y;
}