Trigonometric functions: Difference between revisions

Content deleted Content added
No edit summary
m Fixed lang tags.
Line 4: Line 4:
=={{header|Ada}}==
=={{header|Ada}}==
Ada provides library trig functions which default to radians along with corresponding library functions for which the cycle can be specified. The examples below specify the cycle for degrees and for radians. The output of the inverse trig functions is in units of the specified cycle (degrees or radians).
Ada provides library trig functions which default to radians along with corresponding library functions for which the cycle can be specified. The examples below specify the cycle for degrees and for radians. The output of the inverse trig functions is in units of the specified cycle (degrees or radians).
<lang ada>
<lang ada>with Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions;
use Ada.Numerics.Elementary_Functions;
use Ada.Numerics.Elementary_Functions;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
Line 39: Line 38:
Put (Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)),
Put (Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)),
Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)));
Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)));
end Trig;
end Trig;</lang>
</lang>


Output:
Output:
Line 61: Line 59:


{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algol>
<lang algol68>main:(
main:(
REAL pi = 4 * arc tan(1);
REAL pi = 4 * arc tan(1);
# Pi / 4 is 45 degrees. All answers should be the same. #
# Pi / 4 is 45 degrees. All answers should be the same. #
Line 83: Line 80:
temp := arc tan(tan(radians));
temp := arc tan(tan(radians));
print((temp, " ", temp * 180 / pi, new line))
print((temp, " ", temp * 180 / pi, new line))
)</lang>
)
</lang>
Output:
Output:
<pre>
<pre>
Line 128: Line 124:
=={{header|AWK}}==
=={{header|AWK}}==
awk provides just these bare necessities for trigonometry:
awk provides just these bare necessities for trigonometry:
$ awk 'BEGIN{p4=3.14159/4;print cos(p4),sin(p4),atan2(1,1)}'
<lang awk>$ awk 'BEGIN{p4=3.14159/4;print cos(p4),sin(p4),atan2(1,1)}'
0.707107 0.707106 0.785398
0.707107 0.707106 0.785398</lang>


=={{header|BASIC}}==
=={{header|BASIC}}==
Line 152: Line 148:
=={{header|C}}==
=={{header|C}}==


<lang c>
<lang c>#include <math.h>
#include <math.h>
#include <stdio.h>
#include <stdio.h>


Line 179: Line 174:


return 0;
return 0;
}</lang>
}
</lang>


Output:
Output:
Line 193: Line 187:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>
<lang cpp>#include <iostream>
#include <iostream>
#include <cmath>
#include <cmath>


Line 224: Line 217:


return 0;
return 0;
}</lang>
}
</lang>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Line 274: Line 266:
=={{header|D}}==
=={{header|D}}==
{{trans|C}}
{{trans|C}}
<lang d>
<lang d>import std.stdio, std.math ;
import std.stdio, std.math ;
int main() {
int main() {
Line 300: Line 291:
return 0;
return 0;
}</lang>
}
</lang>


=={{header|E}}==
=={{header|E}}==
Line 332: Line 322:


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth> 45e pi f* 180e f/ \ radians
<lang forth>45e pi f* 180e f/ \ radians

cr fdup fsin f. \ also available: fsincos ( r -- sin cos )
cr fdup fsin f. \ also available: fsincos ( r -- sin cos )
cr fdup fcos f.
cr fdup fcos f.
cr fdup ftan f.
cr fdup ftan f.
cr fdup fasin f.
cr fdup fasin f.
cr fdup facos f.
cr fdup facos f.
cr fatan f. \ also available: fatan2 ( r1 r2 -- atan[r1/r2] )</lang>
cr fatan f. \ also available: fatan2 ( r1 r2 -- atan[r1/r2] )</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==
Trigonometic functions expect arguments in radians so degrees require conversion
Trigonometic functions expect arguments in radians so degrees require conversion
<lang fortran>
<lang fortran>PROGRAM Trig
PROGRAM Trig


REAL pi, dtor, rtod, radians, degrees
REAL pi, dtor, rtod, radians, degrees
Line 361: Line 350:
WRITE(*,*) ATAN(TAN(radians)), ATAN(TAN(degrees*dtor))*rtod
WRITE(*,*) ATAN(TAN(radians)), ATAN(TAN(degrees*dtor))*rtod


END PROGRAM Trig
END PROGRAM Trig</lang>
</lang>
Output:
Output:
0.707107 0.707107
0.707107 0.707107
Line 371: Line 359:
0.785398 45.0000
0.785398 45.0000
The following trigonometric functions are also available
The following trigonometric functions are also available
<lang fortran> ATAN2(y,x) ! Arctangent(y/x), ''-pi < result <= +pi''
<lang fortran>ATAN2(y,x) ! Arctangent(y/x), ''-pi < result <= +pi''
SINH(x) ! Hyperbolic sine
SINH(x) ! Hyperbolic sine
COSH(x) ! Hyperbolic cosine
COSH(x) ! Hyperbolic cosine
TANH(x) ! Hyperbolic tangent</lang>
TANH(x) ! Hyperbolic tangent</lang>


=={{header|Groovy}}==
=={{header|Groovy}}==
Line 403: Line 391:
Trigonometric functions use radians; degrees require conversion.
Trigonometric functions use radians; degrees require conversion.


<lang haskell>
<lang haskell>fromDegrees deg = deg * pi / 180
fromDegrees deg = deg * pi / 180
toDegrees rad = rad * 180 / pi
toDegrees rad = rad * 180 / pi


Line 413: Line 400:
asin 0.5, toDegrees (asin 0.5),
asin 0.5, toDegrees (asin 0.5),
acos 0.5, toDegrees (acos 0.5),
acos 0.5, toDegrees (acos 0.5),
atan 0.5, toDegrees (atan 0.5)]
atan 0.5, toDegrees (atan 0.5)]</lang>
</lang>


=={{header|IDL}}==
=={{header|IDL}}==


<lang idl>deg = 35 ; arbitrary number of degrees
<lang idl>
deg = 35 ; arbitrary number of degrees
rad = !dtor*deg ; system variables !dtor and !radeg convert between rad and deg</lang>
rad = !dtor*deg ; system variables !dtor and !radeg convert between rad and deg</lang>
<lang idl>; the trig functions receive and emit radians:
<lang idl>; the trig functions receive and emit radians:
Line 468: Line 453:
Java's <tt>Math</tt> class contains all six functions and is automatically included as part of the language. The functions all accept radians only, so conversion is necessary when dealing with degrees. The <tt>Math</tt> class also has a <tt>PI</tt> constant for easy conversion.
Java's <tt>Math</tt> class contains all six functions and is automatically included as part of the language. The functions all accept radians only, so conversion is necessary when dealing with degrees. The <tt>Math</tt> class also has a <tt>PI</tt> constant for easy conversion.


<lang java>
<lang java>public class Trig {
public class Trig {
public static void main(String[] args) {
public static void main(String[] args) {
//Pi / 4 is 45 degrees. All answers should be the same.
//Pi / 4 is 45 degrees. All answers should be the same.
Line 490: Line 474:
System.out.println(arctan + " " + Math.toDegrees(arctan));
System.out.println(arctan + " " + Math.toDegrees(arctan));
}
}
}</lang>
}
</lang>


Output:
Output:
Line 507: Line 490:
JavaScript's <tt>Math</tt> class contains all six functions and is automatically included as part of the language. The functions all accept radians only, so conversion is necessary when dealing with degrees. The <tt>Math</tt> class also has a <tt>PI</tt> constant for easy conversion.
JavaScript's <tt>Math</tt> class contains all six functions and is automatically included as part of the language. The functions all accept radians only, so conversion is necessary when dealing with degrees. The <tt>Math</tt> class also has a <tt>PI</tt> constant for easy conversion.


<lang javascript>
<lang javascript>//Pi / 4 is 45 degrees. All answers should be the same.
//Pi / 4 is 45 degrees. All answers should be the same.
var radians = Math.PI / 4;
var radians = Math.PI / 4;
var degrees = 45.0;
var degrees = 45.0;
Line 525: Line 507:
//arctangent
//arctangent
var arctan = Math.atan(Math.tan(radians));
var arctan = Math.atan(Math.tan(radians));
window.alert(arctan + " " + (arctan * 180 / Math.PI));
window.alert(arctan + " " + (arctan * 180 / Math.PI));</lang>
</lang>


=={{header|Logo}}==
=={{header|Logo}}==
[[UCB Logo]] has sine, cosine, and arctangent; each having variants for degrees or radians.
[[UCB Logo]] has sine, cosine, and arctangent; each having variants for degrees or radians.
<lang logo> print sin 45
<lang logo>print sin 45
print cos 45
print cos 45
print arctan 1
print arctan 1
make "pi (radarctan 0 1) * 2 ; based on quadrant if uses two parameters
make "pi (radarctan 0 1) * 2 ; based on quadrant if uses two parameters
print radsin :pi / 4
print radsin :pi / 4
print radcos :pi / 4
print radcos :pi / 4
print 4 * radarctan 1</lang>
print 4 * radarctan 1</lang>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
Maxscript trigonometric functions accept degrees only. The built-ins degToRad and radToDeg allow easy conversion.
Maxscript trigonometric functions accept degrees only. The built-ins degToRad and radToDeg allow easy conversion.
<pre>local radians = pi / 4
<lang maxscript>local radians = pi / 4
local degrees = 45.0
local degrees = 45.0


Line 560: Line 541:
--arctangent
--arctangent
print (atan (tan (radToDeg radians)))
print (atan (tan (radToDeg radians)))
print (atan (tan degrees))</pre>
print (atan (tan degrees))</lang>


=={{header|Metafont}}==
=={{header|Metafont}}==
Line 616: Line 597:


OCaml's preloaded <tt>Pervasives</tt> modules contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees.
OCaml's preloaded <tt>Pervasives</tt> modules contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees.
<lang ocaml>
<lang ocaml>let pi = 4. *. atan 1.
let pi = 4. *. atan 1.


let radians = pi /. 4.
let radians = pi /. 4.
Line 630: Line 610:
Printf.printf "%f %f\n" arccos (arccos *. 180. /. pi);;
Printf.printf "%f %f\n" arccos (arccos *. 180. /. pi);;
let arctan = atan (tan radians);;
let arctan = atan (tan radians);;
Printf.printf "%f %f\n" arctan (arctan *. 180. /. pi);;
Printf.printf "%f %f\n" arctan (arctan *. 180. /. pi);;</lang>
</lang>
Output:
Output:
<pre>
<pre>
Line 758: Line 737:
Python's <tt>math</tt> module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees. The <tt>math</tt> module also has <tt>degrees()</tt> and <tt>radians()</tt> functions for easy conversion.
Python's <tt>math</tt> module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees. The <tt>math</tt> module also has <tt>degrees()</tt> and <tt>radians()</tt> functions for easy conversion.


<lang python>
<lang python>import math
import math


radians = math.pi / 4
radians = math.pi / 4
Line 778: Line 756:
#arctangent
#arctangent
arctan = math.atan(math.tan(radians))
arctan = math.atan(math.tan(radians))
print arctan, math.degrees(arctan)
print arctan, math.degrees(arctan)</lang>
</lang>


Output:
Output:
Line 820: Line 797:
Ruby's <tt>Math</tt> module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees.
Ruby's <tt>Math</tt> module contains all six functions. The functions all accept radians only, so conversion is necessary when dealing with degrees.


<lang ruby>
<lang ruby>radians = Math::PI / 4
radians = Math::PI / 4
degrees = 45.0
degrees = 45.0


Line 846: Line 822:
#arctangent
#arctangent
arctan = Math.atan(Math.tan(radians))
arctan = Math.atan(Math.tan(radians))
puts "#{arctan} #{rad2deg(arctan)}"
puts "#{arctan} #{rad2deg(arctan)}"</lang>
</lang>


Output:
Output:
Line 861: Line 836:
=={{header|Scheme}}==
=={{header|Scheme}}==


<lang scheme>
<lang scheme>(define pi (* 4 (atan 1)))
(define pi (* 4 (atan 1)))


(define radians (/ pi 4))
(define radians (/ pi 4))
Line 898: Line 872:
(display " ")
(display " ")
(display (* arctan (/ 180 pi)))
(display (* arctan (/ 180 pi)))
(newline)
(newline)</lang>
</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==