Trigonometric functions: Difference between revisions

(add E example)
(→‎{{header|OCaml}}: ++ metafont)
Line 473:
print (atan (tan (radToDeg radians)))
print (atan (tan degrees))</pre>
 
=={{header|Metafont}}==
 
Metafont has <code>sind</code> and <code>cosd</code>, which compute sine and cosine of an angle expressed in degree. We need to define the rest.
 
<lang metafont>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</lang>
 
=={{header|OCaml}}==