release 1.11.2
[descalc.git] / 32_math.pm
1 # math for DCT, by Shiar
2
3 # 1.10.4 200410282330 - trig functions from basic menu
4 # 1.10.3 200410152245 - rnd, atan, pi
5 #                     - trigonometry functions seperated
6 # 1.10.2 200410132050 - probability functions: comb, perm, rdz
7 # 1.10.1 200410112340 - adds menu items via addmenu() call
8 # 1.09.2 200410112050 - functions don't handle stack themselves,
9 #                       but behave like real functions
10 # 1.09.1 200410022255 - moved from 1.9 main
11
12 #todo: check for errors, eg division by zero
13
14 use strict;
15 use warnings;
16 use utf8;
17
18 %action = (
19         %action,
20
21         '+'    => [2, sub { $_[1] + $_[0] }], # addition
22         '-'    => [2, sub { $_[1] - $_[0] }], # substraction
23         '*'    => [2, sub { $_[1] * $_[0] }], # multiplication
24         '/'    => [2, sub { $_[1] / $_[0] }], # division
25         'mod'  => [2, sub { $_[1] % $_[0] }], # modulo
26
27         'inv'  => [1, sub { 1 / $_[0] }], # 1/x
28         'sqrt' => [1, sub { sqrt $_[0] }], # square root
29         'sq'   => [1, sub { $_[0] * $_[0] }], # squared
30         '^'    => [2, sub { $_[1] ** $_[0] }], # exponentiation
31         'xroot'=> [2, sub { $_[1] ** (1/$_[0]) }], # x-root of y
32
33         # logarithmic
34         'log'  => [1, sub { log($_[0]) / log(10) }], # logarithm
35         'alog' => [1, sub { 10 ** $_[0] }], # 10^x
36         'ln'   => [1, sub { log $_[0] }], # natural logaritm
37         'lnp1' => [1, sub { log($_[0] + 1) }], # ln(x+1)
38         'exp'  => [1, sub { exp $_[0] }], # e^x
39         'expm' => [1, sub { exp($_[0]) - 1 }], # exp(x)-1
40
41         # binary
42         'and'  => [2, sub { $_[1] & $_[0] }], # bitwise and
43         'or'   => [2, sub { $_[1] | $_[0] }], # bitwise or
44         'xor'  => [2, sub { $_[1] ^ $_[0] }], # bitwise xor
45         'not'  => [2, sub { ~$_[0] }], # bitwise not
46         'sl'   => [1, sub { $_[0] * 2 }], # shift left
47         'sr'   => [1, sub { $_[0] / 2 }], # shift right
48
49         # unclassified
50         '%'    => [2, sub { $_[0] / $_[1] }], # percentage
51 #       '%ch'  => [2, sub { $val{i} = 100*(shift(@_)-$val{i})/$val{i} }], # percentage change
52 #       '%t'   => [2, sub { $val{i} = 100*$val{i}/shift(@_) }], # percentage total
53
54         'abs'  => [1, sub { abs $_[0] }], # absolute
55         'sign' => [1, sub { $_[0] <=> 0 }], # sign
56         'ip'   => [1, sub { int $_[0] }], # integer part
57         'fp'   => [1, sub { $_[0] - int $_[0] }], # fractional part
58
59         'rnd'  => [1, sub { sprintf "%.0f", $_[0] }], # round
60 #       'trnc' => [1, sub { local $_ = 10**$_[0]; $val{i} = int($val{i}*$_)/$_ }], # truncate
61         'floor'=> [1, sub { int $_[0] }], # floor
62         'ceil' => [1, sub { int $_[0]+.9999 }], # ceil
63
64         'min'  => [2, sub { $_[1]<$_[0] ? $_[1] : $_[0] }], # minimum
65         'max'  => [2, sub { $_[1]>$_[0] ? $_[1] : $_[0] }], # maximum
66
67         # number base
68         'dec'  => [-1, sub { $::set{base} = 10; () }], # decimal
69         'bin'  => [-1, sub { $::set{base} = 2; () }], # binary
70         'oct'  => [-1, sub { $::set{base} = 8; () }], # octal
71         'hex'  => [-1, sub { $::set{base} = 16; () }], # hexadecimal
72         'base' => [1, sub { $::set{base} = $_[0]; () }], # alphanumerical
73
74         # probability
75         'comb' => [2, sub {
76                 my $res = 1;
77                 $res *= $_ for $_[1]-$_[0]+1..$_[1];  # (n-r+1)..(n-2)(n-1)n
78                 $res /= $_ for 2..$_[0];  # / r!
79                 $res;  # n!/(r!(n-r)!)
80         }], # combinations
81         'perm' => [2, sub {
82                 my $res = 1;
83                 $res *= $_ for $_[1]-$_[0]+1..$_[1];  # (n-r+1)..(n-2)(n-1)n
84                 $res;  # n!/(n-r)!
85         }], # permutations
86         '!'    => [1, sub { my $res = $_[0]; $res *= $_ for 2..$res-1; $res }], # factor
87         'rand' => [0, sub { rand }], # random value <1
88         'rdz'  => [1, sub { srand $_[0]; () }], # seed randomizer
89 #       'ndist'=> [3], # normal distribution
90 #       'utpn' => [3], # normal distribution
91 #       'utpt' => [1], # student-t distribution
92 #       'utpc' => [2], # chi-square (χ²) distribution
93 #       'utpf' => [3], # F distribution
94 ); # newaction
95
96 addmenu(["main", 0], "math",
97         [qw(basic sq sqrt ^ xroot log alog ln exp)],
98 #       [qw(vector)],
99 #       [qw(matrix)],
100 #       [qw(list)],
101 #       [qw(hyperbolic sinh cosh tanh asinh acosh atanh expm lnp1)],
102         [qw(real % %ch %t min max mod abs sign mant xpon ip fp rnd trnc floor ceil r>d d>r)],
103         [qw(base dec bin oct hex),
104                 [qw(logic and or xor not)],
105                 [qw(bit rl sl asr sr rr)],
106 #               [qw(byte rlb slb srb rrb)],
107         ], # base
108         [qw(probability comb perm ! rand rdz)], # utpc utpf utpn utpt ndist)],
109 #       [qw(fft)],
110 #       [qw(complex)],
111 #       [qw(constants)],
112 ) if defined &addmenu; # addmenu
113
114 return {
115         author  => "Shiar",
116         title   => "basic math",
117         version => "1.10.4",
118 };
119