Trigonometric functions: Difference between revisions

Content added Content deleted
(Updated for D2)
(→‎{{header|AWK}}: Show how to use atan2() to calculate arcsine and arccosine.)
Line 142:
 
=={{header|AWK}}==
awkAwk only provides just<tt>sin()</tt>, these<tt>cos()</tt> and <tt>atan2()</tt>, the three bare necessities for trigonometry. They all use radians. To calculate the other functions, we use these three trigonometric identities:
 
<lang awk>$ awk 'BEGIN{p4=3.14159/4;print cos(p4),sin(p4),atan2(1,1)}'
{|class="wikitable"
0.707107 0.707106 0.785398</lang>
! tangent
! arcsine
! arccosine
|-
| <math>\tan \theta = \frac{\sin \theta}{\cos \theta}</math>
| <math>\tan(\arcsin y) = \frac{y}{\sqrt{1 - y^2}}</math>
| <math>\tan(\arccos x) = \frac{\sqrt{1 - x^2}}{x}</math>
|}
 
With the magic of <tt>atan2()</tt>, arcsine of ''y'' is just <tt>atan2(y, sqrt(1 - y * y))</tt>, and arccosine of ''x'' is just <tt>atan2(sqrt(1 - x * x), x)</tt>. This magic handles the angles ''arcsin(-1)'', ''arcsin 1'' and ''arccos 0'' that have no tangent. This magic also picks the angle in the correct range, so ''arccos(-1/2)'' is ''2*pi/3'' and not some wrong answer like ''-pi/3'' (though ''tan(2*pi/3) = tan(-pi/3) = -sqrt(3)''.)
 
<tt>atan2(y, x)</tt> actually computes the angle of the point ''(x, y)'', in the range ''[-pi, pi]''. When x > 0, this angle is the principle arctangent of ''y/x'', in the range ''(-pi/2, pi/2)''. The calculations for arcsine and arccosine use points on the unit circle at ''x<sup>2</sup> + y<sup>2</sup> = 1''. To calculate arcsine in the range ''[-pi/2, pi/2]'', we take the angle of points on the half-circle ''x = sqrt(1 - y<sup>2</sup>)''. To calculate arccosine in the range ''[0, pi]'', we take the angle of points on the half-circle ''y = sqrt(1 - x<sup>2</sup>)''.
 
<lang awk># tan(x) = tangent of x
function tan(x) {
return sin(x) / cos(x)
}
 
# asin(y) = arcsine of y, domain [-1, 1], range [-pi/2, pi/2]
function asin(y) {
return atan2(y, sqrt(1 - y * y))
}
 
# acos(y) = arccosine of x, domain [-1, 1], range [0, pi]
function acos(x) {
return atan2(sqrt(1 - x * x), x)
}
 
# atan(y) = arctangent of y, range (-pi/2, pi/2)
function atan(y) {
return atan2(y, 1)
}
 
BEGIN {
pi = atan2(0, -1)
degrees = pi / 180
 
print "Using radians:"
print " sin(-pi / 6) =", sin(-pi / 6)
print " cos(3 * pi / 4) =", cos(3 * pi / 4)
print " tan(pi / 3) =", tan(pi / 3)
print " asin(-1 / 2) =", asin(-1 / 2)
print " acos(-sqrt(2) / 2) =", acos(-sqrt(2) / 2)
print " atan(sqrt(3)) =", atan(sqrt(3))
 
print "Using degrees:"
print " sin(-30) =", sin(-30 * degrees)
print " cos(135) =", cos(135 * degrees)
print " tan(60) =", tan(60 * degrees)
print " asin(-1 / 2) =", asin(-1 / 2) / degrees
print " acos(-sqrt(2) / 2) =", acos(-sqrt(2) / 2) / degrees
print " atan(sqrt(3)) =", atan(sqrt(3)) / degrees
}</lang>
 
Output: <pre>Using radians:
sin(-pi / 6) = -0.5
cos(3 * pi / 4) = -0.707107
tan(pi / 3) = 1.73205
asin(-1 / 2) = -0.523599
acos(-sqrt(2) / 2) = 2.35619
atan(sqrt(3)) = 1.0472
Using degrees:
sin(-30) = -0.5
cos(135) = -0.707107
tan(60) = 1.73205
asin(-1 / 2) = -30
acos(-sqrt(2) / 2) = 135
atan(sqrt(3)) = 60</pre>
 
=={{header|BASIC}}==