1 # math for DCT, by Shiar
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
12 #todo: check for errors, eg division by zero
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
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
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
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
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
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
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
64 'min' => [2, sub { $_[1]<$_[0] ? $_[1] : $_[0] }], # minimum
65 'max' => [2, sub { $_[1]>$_[0] ? $_[1] : $_[0] }], # maximum
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
77 $res *= $_ for $_[1]-$_[0]+1..$_[1]; # (n-r+1)..(n-2)(n-1)n
78 $res /= $_ for 2..$_[0]; # / r!
83 $res *= $_ for $_[1]-$_[0]+1..$_[1]; # (n-r+1)..(n-2)(n-1)n
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
96 addmenu(["main", 0], "math",
97 [qw(basic sq sqrt ^ xroot log alog ln exp)],
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)],
108 [qw(probability comb perm ! rand rdz)], # utpc utpf utpn utpt ndist)],
112 ) if defined &addmenu; # addmenu
116 title => "basic math",