Task

If your language has a library or built-in functions for trigonometry, show examples of:

   sine
   cosine
   tangent
   inverses   (of the above)

using the same angle in radians and degrees.

For the non-inverse functions,   each radian/degree pair should use arguments that evaluate to the same angle   (that is, it's not necessary to use the same angle for all three regular functions as long as the two sine calls use the same angle).

For the inverse functions,   use the same number and convert its answer to radians and degrees.

If your language does not have trigonometric functions available or only has some available,   write functions to calculate the functions based on any   known approximation or identity.




Pi := 3.14159;
vardef torad expr x = Pi*x/180 enddef;       % conversions
vardef todeg expr x = 180x/Pi enddef;
vardef sin expr x = sind(todeg(x)) enddef;   % radians version of sind
vardef cos expr x = cosd(todeg(x)) enddef;   % and cosd
 
vardef sign expr x = if x>=0: 1 else: -1 fi enddef; % commodity
 
vardef tand expr x =                      % tan with arg in degree
  if cosd(x) = 0:
    infinity * sign(sind(x))
  else: sind(x)/cosd(x) fi enddef;
vardef tan expr x = tand(todeg(x)) enddef; % arg in rad
 
% INVERSE
 
% the arc having x as tanget is that between x-axis and a line
% from the center to the point (1, x); MF angle says this
vardef atand expr x = angle(1,x) enddef;
vardef atan expr x = torad(atand(x))  enddef;  % rad version
 
% known formula to express asin and acos in function of
% atan; a+-+b stays for sqrt(a^2 - b^2) (defined in plain MF)
vardef asin expr x =  2atan(x/(1+(1+-+x)))  enddef;
vardef acos expr x =  2atan((1+-+x)/(1+x))  enddef;
 
vardef asind expr x =  todeg(asin(x))  enddef; % degree versions
vardef acosd expr x =  todeg(acos(x))  enddef;
 
% commodity
def outcompare(expr a, b) = message decimal a & " = " & decimal b enddef;
 
% output tests
outcompare(torad(60), Pi/3);
outcompare(todeg(Pi/6), 30);
 
outcompare(Pi/3, asin(sind(60)));
outcompare(30, acosd(cos(Pi/6)));
outcompare(45, atand(tand(45)));
outcompare(Pi/4, atan(tand(45)));
 
outcompare(sin(Pi/3), sind(60));
outcompare(cos(Pi/4), cosd(45));
outcompare(tan(Pi/3), tand(60));
 
end