# trigonometry for DCT, by Shiar

# 1.11.2 200411032120 - check for menu module before addmenu()
# 1.11.1 200410282330 - cardial mode setting; rad/deg to switch to radians/degrees
#                     - convert from/to radians for trig commands if rad mode set
# 1.11.0 200410152320 - a?(sin|cos|tan)h? actions from math; links in main submenu trig

use strict;
use warnings;

my $pi = atan2(1, 1) * 4;

$set{card} = 1;  # degrees radians grades

%action = (
	%action,

	'pi'   => [0, sub { $pi }], # pi constant

	'deg'  => [-1, sub { $set{card} = 1 }], # set degrees
	'rad'  => [-1, sub { $set{card} = 2 }], # set radians

	# trigonometric
	'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 { atan2($_[0], 1) }], # arctangent

	# hyperbolic
	'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
); # action

push @{$hook{preaction}}, sub {
	return unless $set{card}==2;
	# convert user input from radians if necessary
	$stack[0] /= 360/$pi if $_[1] =~ /^(?:sin|cos|tan)h?$/;
}; # preaction
push @{$hook{postaction}}, sub {
	return unless $set{card}==2;
	# convert command output to radians if necessary
	$stack[0] *= 360/$pi if $_[1] =~ /^a(?:sin|cos|tan)h?$/;
}; # postaction

addmenu(["main", 0], "trig", #todo: in math, not in main
	qw(sin cos tan asin acos atan),
	qw(sinh cosh tanh asinh acosh atanh),
	qw(expm lnp1),
) if defined &addmenu;

return {
	author  => "Shiar",
	title   => "trigonometry",
	version => "1.11.2",
};

