Trigonometric functions: Difference between revisions

Content added Content deleted
(-> IDL)
(added scheme)
Line 258: Line 258:


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.
let pi = 4. *. atan 1.
<ocaml>let pi = 4. *. atan 1.

let radians = pi /. 4.
let radians = pi /. 4.
let degrees = 45.
let degrees = 45.;;

Printf.printf "%f %f\n" (sin radians) (sin (degrees *. pi /. 180.));;
let () =
Printf.printf "%f %f\n" (sin radians) (sin (degrees *. pi /. 180.));
Printf.printf "%f %f\n" (cos radians) (cos (degrees *. pi /. 180.));;
Printf.printf "%f %f\n" (cos radians) (cos (degrees *. pi /. 180.));
Printf.printf "%f %f\n" (tan radians) (tan (degrees *. pi /. 180.));;
let arcsin = asin (sin radians);;
Printf.printf "%f %f\n" (tan radians) (tan (degrees *. pi /. 180.));
let arcsin = asin (sin radians)
Printf.printf "%f %f\n" arcsin (arcsin *. 180. /. pi);;
and arccos = acos (cos radians)
let arccos = acos (cos radians);;
Printf.printf "%f %f\n" arccos (arccos *. 180. /. pi);;
and arctan = atan (tan radians) in
let arctan = atan (tan radians);;
Printf.printf "%f %f\n" arcsin (arcsin *. 180. /. pi);
Printf.printf "%f %f\n" arccos (arccos *. 180. /. pi);
Printf.printf "%f %f\n" arctan (arctan *. 180. /. pi);;</ocaml>
Printf.printf "%f %f\n" arctan (arctan *. 180. /. pi)
Output:
Output:
<pre>
<pre>
Line 285: Line 284:
=={{header|Perl}}==
=={{header|Perl}}==
{{works with|Perl|5.8.8}}
{{works with|Perl|5.8.8}}
use Math::Trig;
<perl>use Math::Trig;

$angle_degrees = 45;
$angle_degrees = 45;
$angle_radians = pi / 4;
$angle_radians = pi / 4;

print sin($angle_radians).' '.sin(deg2rad($angle_degrees))."\n";
print sin($angle_radians).' '.sin(deg2rad($angle_degrees))."\n";
print cos($angle_radians).' '.cos(deg2rad($angle_degrees))."\n";
print cos($angle_radians).' '.cos(deg2rad($angle_degrees))."\n";
print tan($angle_radians).' '.tan(deg2rad($angle_degrees))."\n";
print tan($angle_radians).' '.tan(deg2rad($angle_degrees))."\n";
print cot($angle_radians).' '.cot(deg2rad($angle_degrees))."\n";
print cot($angle_radians).' '.cot(deg2rad($angle_degrees))."\n";
$asin = asin(sin($angle_radians));
$asin = asin(sin($angle_radians));
print $asin.' '.rad2deg($asin)."\n";
print $asin.' '.rad2deg($asin)."\n";
$acos = acos(cos($angle_radians));
$acos = acos(cos($angle_radians));
print $acos.' '.rad2deg($acos)."\n";
print $acos.' '.rad2deg($acos)."\n";
$atan = atan(tan($angle_radians));
$atan = atan(tan($angle_radians));
print $atan.' '.rad2deg($atan)."\n";
print $atan.' '.rad2deg($atan)."\n";
$acot = acot(cot($angle_radians));
$acot = acot(cot($angle_radians));
print $acot.' '.rad2deg($acot)."\n";
print $acot.' '.rad2deg($acot)."\n";</perl>


Output:
Output:
Line 314: Line 313:
0.785398163397448 45
0.785398163397448 45
</pre>
</pre>

=={{header|Scheme}}==

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

(define radians (/ pi 4))
(define degrees 45)

(display (sin radians))
(display " ")
(display (sin (* degrees (/ pi 180))))
(newline)

(display (cos radians))
(display " ")
(display (cos (* degrees (/ pi 180))))
(newline)

(display (tan radians))
(display " ")
(display (tan (* degrees (/ pi 180))))
(newline)
(define arcsin (asin (sin radians)))
(display arcsin)
(display " ")
(display (* arcsin (/ 180 pi)))
(newline)

(define arccos (acos (cos radians)))
(display arccos)
(display " ")
(display (* arccos (/ 180 pi)))
(newline)

(define arctan (atan (tan radians)))
(display arctan)
(display " ")
(display (* arctan (/ 180 pi)))
(newline)</scheme>