# menu 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

use strict;
use warnings;
use utf8;

my %newaction = (
	'+'    => [2, sub { $_[1] + $_[0] }], # addition
	'-'    => [2, sub { $_[1] - $_[0] }], # substraction
	'*'    => [2, sub { $_[1] * $_[0] }], # multiplication
	'/'    => [2, sub { $_[1] / $_[0] }], # division
	'mod'  => [2, sub { $_[1] % $_[0] }], # modulo

	'inv'  => [1, sub { 1 / $_[0] }], # 1/x
	'sqrt' => [1, sub { sqrt $_[0] }], # square root
	'sq'   => [1, sub { $_[0] * $_[0] }], # squared
	'^'    => [2, sub { $_[1] ** $_[0] }], # exponentiation
	'xroot'=> [2, sub { $_[1] ** (1/$_[0]) }], # x-root of y

	'log'  => [1, sub { log($_[0]) / log(10) }], # logarithm
	'alog' => [1, sub { 10 ** $_[0] }], # 10^x
	'ln'   => [1, sub { log $_[0] }], # natural logaritm
	'lnp1' => [1, sub { log($_[0] + 1) }], # ln(x+1)
	'exp'  => [1, sub { exp $_[0] }], # e^x
	'expm' => [1, sub { exp($_[0]) - 1 }], # exp(x)-1

	'sin'  => [1, sub { sin $_[0] }], # sine
	'asin' => [1, sub { atan2($_[0], sqrt(1 - $_[0]*$_[0])) }], # inverse sine
	'cos'  => [1, sub { cos $_[0] }], # cosine
	'acos' => [1, sub { atan2(sqrt(1 - $_[0]*$_[0]), $_[0]) }], # inverse cosine
	'tan'  => [1, sub { sin($_[0]) / cos($_[0]) }], # tangent
#	'atan' => [1, sub { }], # arctangent

	'sinh' => [1, sub { (exp($_[0]) - exp(-$_[0])) / 2 }], # hyperbolic sine
	'cosh' => [1, sub { (exp($_[0]) + exp(-$_[0])) / 2 }], # hyperbolic cosine
	'tanh' => [1, sub { (exp($_[0]) - exp(-$_[0])) / (exp($_[0]) + exp(-$_[0])) }], # hyperbolic tangent (sinh/cosh)
	'asinh'=> [1, sub { log(sqrt($_[0]**2+1) + $_[0]) }], # inverse hyperbolic sine
	'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

	'and'  => [2, sub { $_[1] & $_[0] }], # bitwise and
	'or'   => [2, sub { $_[1] | $_[0] }], # bitwise or
	'xor'  => [2, sub { $_[1] ^ $_[0] }], # bitwise xor
	'not'  => [2, sub { ~$_[0] }], # bitwise not
	'sl'   => [1, sub { $_[0] * 2 }], # shift left
	'sr'   => [1, sub { $_[0] / 2 }], # shift right

	'abs'  => [1, sub { abs $_[0] }], # absolute #todo
	'sign' => [1, sub { $_[0] <=> 0 }], # sign
	'ip'   => [1, sub { int $_[0] }], # integer part
	'fp'   => [1, sub { $_[0] - int $_[0] }], # fractional part

#	'rnd'  => [1, sub { local $_ = 10**$_[0]; $val{i} = int(($val{i}+.5)*$_)/$_ }], # round
#	'trnc' => [1, sub { local $_ = 10**$_[0]; $val{i} = int($val{i}*$_)/$_ }], # truncate
	'floor'=> [1, sub { int $_[0] }], # floor
	'ceil' => [1, sub { int $_[0]+.9999 }], # ceil

	'min'  => [2, sub { $_[1]<$_[0] ? $_[1] : $_[0] }], # minimum
	'max'  => [2, sub { $_[1]>$_[0] ? $_[1] : $_[0] }], # maximum

	'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

	'!'    => [1, sub { my $res = $_[0]; $res *= $_ for 2..$res-1; $res }], # factor
	'rand' => [0, sub { rand }], # random value <1
); # newaction

#while (my ($cmd, $val) = each %newaction) {
#	$action{$cmd} = $val;
#}

$action{$_} = $newaction{$_} for keys %newaction;

1;

