release 1.10.6
[descalc.git] / 31_math.pm
similarity index 66%
rename from math.pm
rename to 31_math.pm
index b83ce162d56d3801bcabd2f04c4b009d49c3ea90..dd4d7d8f5a247fcee933896017e3b0d93f4d0140 100644 (file)
--- a/math.pm
@@ -1,8 +1,10 @@
-# menu for DCT, by Shiar
+# math for DCT, by Shiar
 
-# 1.09.1 2004-10-02 22:55 - moved from 1.9 main
-# 1.09.2 2004-10-11 20:50 - functions don't handle stack themselves,
-#                           but behave like real functions
+# 1.09.1 200410022255 - moved from 1.9 main
+# 1.09.2 200410112050 - functions don't handle stack themselves,
+#                       but behave like real functions
+# 1.10.1 200410112340 - adds menu items via addmenu() call
+#     .2 200410132050 - probability functions: comb, perm, rdz
 
 use strict;
 use warnings;
@@ -21,6 +23,7 @@ my %newaction = (
        '^'    => [2, sub { $_[1] ** $_[0] }], # exponentiation
        'xroot'=> [2, sub { $_[1] ** (1/$_[0]) }], # x-root of y
 
+       # logarithmic
        'log'  => [1, sub { log($_[0]) / log(10) }], # logarithm
        'alog' => [1, sub { 10 ** $_[0] }], # 10^x
        'ln'   => [1, sub { log $_[0] }], # natural logaritm
@@ -28,6 +31,7 @@ my %newaction = (
        'exp'  => [1, sub { exp $_[0] }], # e^x
        'expm' => [1, sub { exp($_[0]) - 1 }], # exp(x)-1
 
+       # hyperbolic
        'sin'  => [1, sub { sin $_[0] }], # sine
        'asin' => [1, sub { atan2($_[0], sqrt(1 - $_[0]*$_[0])) }], # inverse sine
        'cos'  => [1, sub { cos $_[0] }], # cosine
@@ -42,10 +46,7 @@ my %newaction = (
        'acosh'=> [1, sub { log(sqrt($_[0]**2-1) + $_[0]) }], # inverse hyperbolic cosine
        'atanh'=> [1, sub { log((1+$_[0]) / (1-$_[0])) / 2 }], # inverse hyperbolic tangent
 
-       '%'    => [2, sub { $_[0] / $_[1] }], # percentage
-#      '%ch'  => [2, sub { $val{i} = 100*(shift(@_)-$val{i})/$val{i} }], # percentage change
-#      '%t'   => [2, sub { $val{i} = 100*$val{i}/shift(@_) }], # percentage total
-
+       # binary
        'and'  => [2, sub { $_[1] & $_[0] }], # bitwise and
        'or'   => [2, sub { $_[1] | $_[0] }], # bitwise or
        'xor'  => [2, sub { $_[1] ^ $_[0] }], # bitwise xor
@@ -53,6 +54,11 @@ my %newaction = (
        'sl'   => [1, sub { $_[0] * 2 }], # shift left
        'sr'   => [1, sub { $_[0] / 2 }], # shift right
 
+       # unclassified
+       '%'    => [2, sub { $_[0] / $_[1] }], # percentage
+#      '%ch'  => [2, sub { $val{i} = 100*(shift(@_)-$val{i})/$val{i} }], # percentage change
+#      '%t'   => [2, sub { $val{i} = 100*$val{i}/shift(@_) }], # percentage total
+
        'abs'  => [1, sub { abs $_[0] }], # absolute #todo
        'sign' => [1, sub { $_[0] <=> 0 }], # sign
        'ip'   => [1, sub { int $_[0] }], # integer part
@@ -66,21 +72,59 @@ my %newaction = (
        'min'  => [2, sub { $_[1]<$_[0] ? $_[1] : $_[0] }], # minimum
        'max'  => [2, sub { $_[1]>$_[0] ? $_[1] : $_[0] }], # maximum
 
+       # number base
        'dec'  => [-1, sub { $::set{base} = 10; () }], # decimal
        'bin'  => [-1, sub { $::set{base} = 2; () }], # binary
        'oct'  => [-1, sub { $::set{base} = 8; () }], # octal
        'hex'  => [-1, sub { $::set{base} = 16; () }], # hexadecimal
        'base' => [1, sub { $::set{base} = $_[0]; () }], # alphanumerical
 
+       # probability
+       'comb' => [2, sub {
+               my $res = 1;
+               $res *= $_ for $_[1]-$_[0]+1..$_[1];  # (n-r+1)..(n-2)(n-1)n
+               $res /= $_ for 2..$_[0];  # / r!
+               $res;  # n!/(r!(n-r)!)
+       }], # combinations
+       'perm' => [2, sub {
+               my $res = 1;
+               $res *= $_ for $_[1]-$_[0]+1..$_[1];  # (n-r+1)..(n-2)(n-1)n
+               $res;  # n!/(n-r)!
+       }], # permutations
        '!'    => [1, sub { my $res = $_[0]; $res *= $_ for 2..$res-1; $res }], # factor
        'rand' => [0, sub { rand }], # random value <1
+       'rdz'  => [1, sub { srand $_[0]; () }], # seed randomizer
+#      'ndist'=> [3], # normal distribution
+#      'utpn' => [3], # normal distribution
+#      'utpt' => [1], # student-t distribution
+#      'utpc' => [2], # chi-square (χ²) distribution
+#      'utpf' => [3], # F distribution
 ); # newaction
 
-#while (my ($cmd, $val) = each %newaction) {
-#      $action{$cmd} = $val;
-#}
-
+#while (my ($cmd, $val) = each %newaction) {$action{$cmd} = $val}
 $action{$_} = $newaction{$_} for keys %newaction;
 
-1;
+addmenu(["main", 0], "math",
+       [qw(basic log alog ln exp sin cos tan asin acos atan sq sqrt ^ xroot)],
+#      [qw(vector)],
+#      [qw(matrix)],
+#      [qw(list)],
+       [qw(hyperbolic sinh cosh tanh asinh acosh atanh expm lnp1)],
+       [qw(real % %ch %t min max mod abs sign mant xpon ip fp rnd trnc floor ceil r>d d>r)],
+       [qw(base dec bin oct hex),
+               [qw(logic and or xor not)],
+               [qw(bit rl sl asr sr rr)],
+#              [qw(byte rlb slb srb rrb)],
+       ], # base
+       [qw(probability comb perm ! rand rdz)], # utpc utpf utpn utpt ndist)],
+#      [qw(fft)],
+#      [qw(complex)],
+#      [qw(constants)],
+) if defined &addmenu; # addmenu
+
+return {
+       author  => "Shiar",
+       title   => "basic math",
+       version => "1.10.2",
+};