Jump to content

Trigonometric functions: Difference between revisions

m
Fixed lang tags.
No edit summary
m (Fixed lang tags.)
Line 4:
=={{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).
<lang ada>with Ada.Numerics.Elementary_Functions;
with Ada.Numerics.Elementary_Functions;
use Ada.Numerics.Elementary_Functions;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
Line 39 ⟶ 38:
Put (Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)),
Arccot (X => Cot (Angle_Degrees, Degrees_Cycle)));
end Trig;</lang>
</lang>
 
Output:
Line 61 ⟶ 59:
 
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algolalgol68>main:(
main:(
REAL pi = 4 * arc tan(1);
# Pi / 4 is 45 degrees. All answers should be the same. #
Line 83 ⟶ 80:
temp := arc tan(tan(radians));
print((temp, " ", temp * 180 / pi, new line))
)</lang>
)
</lang>
Output:
<pre>
Line 128 ⟶ 124:
=={{header|AWK}}==
awk provides just these bare necessities for trigonometry:
<lang awk>$ awk 'BEGIN{p4=3.14159/4;print cos(p4),sin(p4),atan2(1,1)}'
0.707107 0.707106 0.785398</lang>
 
=={{header|BASIC}}==
Line 152 ⟶ 148:
=={{header|C}}==
 
<lang c>#include <math.h>
#include <math.h>
#include <stdio.h>
 
Line 179 ⟶ 174:
 
return 0;
}</lang>
}
</lang>
 
Output:
Line 193 ⟶ 187:
 
=={{header|C++}}==
<lang cpp>#include <iostream>
#include <iostream>
#include <cmath>
 
Line 224 ⟶ 217:
 
return 0;
}</lang>
}
</lang>
 
=={{header|C sharp|C#}}==
Line 274 ⟶ 266:
=={{header|D}}==
{{trans|C}}
<lang d>import std.stdio, std.math ;
import std.stdio, std.math ;
int main() {
Line 300 ⟶ 291:
return 0;
}</lang>
}
</lang>
 
=={{header|E}}==
Line 332 ⟶ 322:
 
=={{header|Forth}}==
<lang forth> 45e pi f* 180e f/ \ radians
 
cr fdup fsin f. \ also available: fsincos ( r -- sin cos )
cr fdup fcos f.
cr fdup ftan f.
cr fdup fasin f.
cr fdup facos f.
cr fatan f. \ also available: fatan2 ( r1 r2 -- atan[r1/r2] )</lang>
 
=={{header|Fortran}}==
Trigonometic functions expect arguments in radians so degrees require conversion
<lang fortran>PROGRAM Trig
PROGRAM Trig
 
REAL pi, dtor, rtod, radians, degrees
Line 361 ⟶ 350:
WRITE(*,*) ATAN(TAN(radians)), ATAN(TAN(degrees*dtor))*rtod
 
END PROGRAM Trig</lang>
</lang>
Output:
0.707107 0.707107
Line 371 ⟶ 359:
0.785398 45.0000
The following trigonometric functions are also available
<lang fortran> ATAN2(y,x) ! Arctangent(y/x), ''-pi < result <= +pi''
SINH(x) ! Hyperbolic sine
COSH(x) ! Hyperbolic cosine
TANH(x) ! Hyperbolic tangent</lang>
 
=={{header|Groovy}}==
Line 403 ⟶ 391:
Trigonometric functions use radians; degrees require conversion.
 
<lang haskell>fromDegrees deg = deg * pi / 180
fromDegrees deg = deg * pi / 180
toDegrees rad = rad * 180 / pi
 
Line 413 ⟶ 400:
asin 0.5, toDegrees (asin 0.5),
acos 0.5, toDegrees (acos 0.5),
atan 0.5, toDegrees (atan 0.5)]</lang>
</lang>
 
=={{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>
<lang idl>; the trig functions receive and emit radians:
Line 468 ⟶ 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.
 
<lang java>public class Trig {
public class Trig {
public static void main(String[] args) {
//Pi / 4 is 45 degrees. All answers should be the same.
Line 490 ⟶ 474:
System.out.println(arctan + " " + Math.toDegrees(arctan));
}
}</lang>
}
</lang>
 
Output:
Line 507 ⟶ 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.
 
<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 degrees = 45.0;
Line 525 ⟶ 507:
//arctangent
var arctan = Math.atan(Math.tan(radians));
window.alert(arctan + " " + (arctan * 180 / Math.PI));</lang>
</lang>
 
=={{header|Logo}}==
[[UCB Logo]] has sine, cosine, and arctangent; each having variants for degrees or radians.
<lang logo> print sin 45
print cos 45
print arctan 1
make "pi (radarctan 0 1) * 2 ; based on quadrant if uses two parameters
print radsin :pi / 4
print radcos :pi / 4
print 4 * radarctan 1</lang>
 
=={{header|MAXScript}}==
Maxscript trigonometric functions accept degrees only. The built-ins degToRad and radToDeg allow easy conversion.
<prelang maxscript>local radians = pi / 4
local degrees = 45.0
 
Line 560 ⟶ 541:
--arctangent
print (atan (tan (radToDeg radians)))
print (atan (tan degrees))</prelang>
 
=={{header|Metafont}}==
Line 616 ⟶ 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.
<lang ocaml>let pi = 4. *. atan 1.
let pi = 4. *. atan 1.
 
let radians = pi /. 4.
Line 630 ⟶ 610:
Printf.printf "%f %f\n" arccos (arccos *. 180. /. pi);;
let arctan = atan (tan radians);;
Printf.printf "%f %f\n" arctan (arctan *. 180. /. pi);;</lang>
</lang>
Output:
<pre>
Line 758 ⟶ 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.
 
<lang python>import math
import math
 
radians = math.pi / 4
Line 778 ⟶ 756:
#arctangent
arctan = math.atan(math.tan(radians))
print arctan, math.degrees(arctan)</lang>
</lang>
 
Output:
Line 820 ⟶ 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.
 
<lang ruby>radians = Math::PI / 4
radians = Math::PI / 4
degrees = 45.0
 
Line 846 ⟶ 822:
#arctangent
arctan = Math.atan(Math.tan(radians))
puts "#{arctan} #{rad2deg(arctan)}"</lang>
</lang>
 
Output:
Line 861 ⟶ 836:
=={{header|Scheme}}==
 
<lang scheme>(define pi (* 4 (atan 1)))
(define pi (* 4 (atan 1)))
 
(define radians (/ pi 4))
Line 898 ⟶ 872:
(display " ")
(display (* arctan (/ 180 pi)))
(newline)</lang>
</lang>
 
=={{header|Tcl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.