Trigonometric functions: Difference between revisions
Added Easylang
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(Added Easylang) |
||
(9 intermediate revisions by 8 users not shown) | |||
Line 654:
PRINT ATN(TAN(radians)) + " " + ATN(TAN(radians)) * 180 / pi 'arctan</syntaxhighlight>
==={{header|Applesoft BASIC}}===
The arcsine and arccoscine functions, while not intrinsic to Applesoft BASIC, are
calculated using the existing BASIC functions and implemented as FN ASN and FN ACS
using the DEF FN function.
<syntaxhighlight lang="gwbasic"> 100 TAU = 8 * ATN (1)
110 RAD = TAU / 8
120 DEG = 45.0
130 DEF FN RAD(DEG) = DEG * TAU / 360
140 DEF FN DEG(RAD) = RAD / TAU * 360
150 DEF FN ASN(RAD) = ATN (RAD / SQR ( - RAD * RAD + 1))
160 DEF FN ACS(RAD) = - ATN (RAD / SQR ( - RAD * RAD + 1)) + TAU / 4
170 PRINT " SINE: " SIN (RAD);: HTAB (25): PRINT SIN ( FN RAD(DEG))
180 PRINT " COSINE: " COS (RAD);: HTAB (25): PRINT COS ( FN RAD(DEG))
190 PRINT " TANGENT: " TAN (RAD);: HTAB (25): PRINT TAN ( FN RAD(DEG))
200 ARC = FN ASN( SIN (RAD))
210 PRINT " ARCSINE: "ARC;: HTAB (25): PRINT FN DEG(ARC)
220 ARC = FN ACS( COS (RAD))
230 PRINT " ARCCOSINE: "ARC;: HTAB (25): PRINT FN DEG(ARC)
240 ARC = ATN ( TAN (RAD))
250 PRINT " ARCTANGENT: "ARC;: HTAB (25): PRINT FN DEG(ARC);</syntaxhighlight>
{{out}}
<pre>
SINE: .707106781 .707106781
COSINE: .707106781 .707106781
TANGENT: 1 1
ARCSINE: .785398163 45
ARCCOSINE: .785398164 45.0000001
ARCTANGENT: .785398163 45
</pre>
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">radian = pi / 4
Line 727 ⟶ 756:
print "Arc Tangent : ", temp, tab$, temp * 180 / pi
end</syntaxhighlight>
=={{header|bc}}==
Line 1,078 ⟶ 1,106:
Arccosine: 0.78539816339744830961 45.00000000000000000000
Arctangent: 0.78539816339744830961 45.00000000000000000000</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
procedure ShowTrigFunctions(Memo: TMemo);
const AngleDeg = 45.0;
var AngleRad,ArcSine,ArcCosine,ArcTangent: double;
begin
AngleRad:=DegToRad(AngleDeg);
Memo.Lines.Add(Format('Angle: Degrees: %3.5f Radians: %3.6f',[AngleDeg,AngleRad]));
Memo.Lines.Add('-------------------------------------------------');
Memo.Lines.Add(Format('Sine: Degrees: %3.6f Radians: %3.6f',[sin(DegToRad(AngleDeg)),sin(AngleRad)]));
Memo.Lines.Add(Format('Cosine: Degrees: %3.6f Radians: %3.6f',[cos(DegToRad(AngleDeg)),cos(AngleRad)]));
Memo.Lines.Add(Format('Tangent: Degrees: %3.6f Radians: %3.6f',[tan(DegToRad(AngleDeg)),tan(AngleRad)]));
ArcSine:=ArcSin(Sin(AngleRad));
Memo.Lines.Add(Format('Arcsine: Degrees: %3.6f Radians: %3.6f',[DegToRad(ArcSine),ArcSine]));
ArcCosine:=ArcCos(cos(AngleRad));
Memo.Lines.Add(Format('Arccosine: Degrees: %3.6f Radians: %3.6f',[DegToRad(ArcCosine),ArcCosine]));
ArcTangent:=ArcTan(tan(AngleRad));
Memo.Lines.Add(Format('Arctangent: Degrees: %3.6f Radians: %3.6f',[DegToRad(ArcTangent),ArcTangent]));
end;
</syntaxhighlight>
{{out}}
<pre>
Angle: Degrees: 45.00000 Radians: 0.785398
-------------------------------------------------
Sine: Degrees: 0.707107 Radians: 0.707107
Cosine: Degrees: 0.707107 Radians: 0.707107
Tangent: Degrees: 1.000000 Radians: 1.000000
Arcsine: Degrees: 0.013708 Radians: 0.785398
Arccosine: Degrees: 0.013708 Radians: 0.785398
Arctangent: Degrees: 0.013708 Radians: 0.785398
Elapsed Time: 9.118 ms.
</pre>
=={{header|E}}==
Line 1,106 ⟶ 1,179:
0.7853981633974483 45.0
0.7853981633974483 45.0
=={{header|EasyLang}}==
<syntaxhighlight>
r = pi / 4
d = 45
#
func r2d r .
return r / pi * 180
.
func d2r d .
return d * pi / 180
.
#
numfmt 4 0
print sin d & " " & sin r2d r
print cos d & " " & cos r2d r
print tan d & " " & tan r2d r
print ""
h = asin sin d
print h & " " & d2r h
h = acos cos d
print h & " " & d2r h
h = atan tan d
print h & " " & d2r h
</syntaxhighlight>
{{out}}
<pre>
0.7071 0.7071
0.7071 0.7071
1.0000 1.0000
45.0000 0.7854
45 0.7854
45 0.7854
</pre>
=={{header|Elena}}==
Line 1,566 ⟶ 1,675:
Arc Cosine : 0.7853981633974483 45
Arc Tangent : 0.7853981633974483 45
</pre>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
void local fn DoIt
double degrees = 45.0
double radians = degrees * M_PI / 180
NSLog(@"%f, %f",sin(radians),sin(degrees * M_PI / 180))
NSLog(@"%f, %f",cos(radians),cos(degrees * M_PI / 180))
NSLog(@"%f, %f",tan(radians),tan(degrees * M_PI / 180))
NSLog(@"%f, %f",asin(sin(radians)),asin(sin(radians)) * 180 / M_PI)
NSLog(@"%f, %f",acos(cos(radians)),acos(cos(radians)) * 180 / M_PI)
NSLog(@"%f, %f",atan(tan(radians)),atan(tan(radians)) * 180 / M_PI)
end fn
fn DoIt
HandleEvents
</syntaxhighlight>
{{out}}
<pre>
0.707107, 0.707107
0.707107, 0.707107
1.000000, 1.000000
0.785398, 45.000000
0.785398, 45.000000
0.785398, 45.000000
</pre>
Line 2,009 ⟶ 2,150:
=={{header|Kotlin}}==
<syntaxhighlight lang="
fun main() {
fun Double.toDegrees() = this * 180 / PI
val angle = PI / 4
println("angle = $angle rad = ${angle.toDegrees()}°")
val sine = sin(angle)
println("sin(angle) = $sine")
val cosine = cos(angle)
println("cos(angle) = $cosine")
val tangent = tan(angle)
println("tan(angle) = $tangent")
println()
val asin = asin(sine)
println("asin(sin(angle)) = $asin rad = ${asin.toDegrees()}°")
val
println("acos(cos(angle)) = $acos rad = ${acos.toDegrees()}°")
val
println("atan(tan(angle)) = $atan rad = ${atan.toDegrees()}°")
}</syntaxhighlight>
{{out}}
<pre>
angle = 0.7853981633974483 rad = 45.0°
sin(angle) = 0.7071067811865475
cos(angle) = 0.7071067811865476
tan(angle) = 0.9999999999999999
asin(sin(angle)) = 0.7853981633974482 rad = 44.99999999999999°
acos(cos(angle)) = 0.7853981633974483 rad = 45.0°
atan(tan(angle)) = 0.7853981633974483 rad = 45.0°
</pre>
Line 3,857 ⟶ 3,995:
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">90</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">180</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">*</span><span style="color: #
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">tan</span><span style="color: #0000FF;">(</span><span style="color: #
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">tan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">45</span><span style="color: #0000FF;">*</span><span style="color: #
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arcsin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arcsin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">180</span><span style="color: #0000FF;">/</span><span style="color: #
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arccos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arccos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">180</span><span style="color: #0000FF;">/</span><span style="color: #
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">4</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">arctan</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">180</span><span style="color: #0000FF;">/</span><span style="color: #
<!--</syntaxhighlight>-->
{{out}}
Line 4,702 ⟶ 4,841:
</syntaxhighlight>
=={{header|RPL}}==
RPL has somewhere a system flag that defines if arguments passed to trigonometric functions are in degrees or radians. The words <code>DEG</code> and <code>RAD</code> set the flag appropriately.
We can therefore answer the task so:
π 4 / →NUM 'XRAD' STO
45 'XDEG' STO
XRAD RAD SIN XDEG DEG SIN
which will return <code>.707106781187</code> 2 times.
Another way is to stay in the same trigonometric mode and use <code>D→R</code> or <code>R→D</code> conversion words. This is the way used below:
RAD
π 4 / →NUM SIN 45 D→R SIN
π 3 / →NUM COS 60 D→R COS
π 6 / →NUM TAN 30 D→R TAN
{{out}}
<pre>
6: .707106781187
5: .707106781187
4: .499999999997
3: .499999999997
2: .577350269189
1: .577350269189
</pre>
As we have now in the stack the 6 values to be inversed, let's call the required functions in reverse order. The <code>6 ROLLD</code> instruction pushes the number from level 1 to level 6 of the stack, making thus the next number available for inversion.
ATAN R→D 6 ROLLD
ATAN 6 ROLLD
ACOS R→D 6 ROLLD
ACOS 6 ROLLD
ASIN R→D 6 ROLLD
ASIN 6 ROLLD
{{out}}
<pre>
6: .785398163397
5: 45
4: 1.0471975512
3: 60.0000000002
2: .523598775598
1: 30
</pre>
Calculations made with a HP-28S. Emulator has better precision and returns 60 for <code>60 D→R COS ACOS R→D</code>
=={{header|Ruby}}==
Line 5,060 ⟶ 5,237:
0.785398163397448 45
</pre>
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "trig" )
@( description, "If your language has a library or built-in " )
@( description, "functions for trigonometry, show examples of: ")
@( description, "sine, cosine, tangent, inverses (of the above) " )
@( description, "using the same angle in radians and degrees." )
@( description, "" )
@( description, "For the non-inverse functions, each radian/" )
@( description, "degree pair should use arguments that evaluate to " )
@( description, "the same angle (that is, it's not necessary to " )
@( description, "use the same angle for all three regular " )
@( description, "functions as long as the two sine calls use the " )
@( description, "same angle). For the inverse functions, use " )
@( description, "the same number and convert its answer to radians " )
@( description, "and degrees." )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Trigonometric_functions" );
pragma license( unrestricted );
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
procedure trig is
degrees_cycle : constant float := 360.0;
radians_cycle : constant float := 2.0 * float( numerics.pi );
angle_degrees : constant float := 45.0;
angle_radians : constant float := float( numerics.pi ) / 4.0;
begin
put( "Sin " )
@( numerics.sin( angle_degrees, degrees_cycle ) )
@( numerics.sin( angle_radians, radians_cycle ) );
new_line;
put( "Cos " )
@( numerics.cos( angle_degrees, degrees_cycle ) )
@( numerics.cos( angle_radians, radians_cycle ) );
new_line;
put( "Tan " )
@( numerics.tan( angle_degrees, degrees_cycle ) )
@( numerics.tan( angle_radians, radians_cycle ) );
new_line;
put( "Cot " )
@( numerics.cot( angle_degrees, degrees_cycle ) )
@( numerics.cot( angle_radians, radians_cycle ) );
new_line;
put( "Arcsin" )
@( numerics.arcsin( numerics.sin( angle_degrees, degrees_cycle ), degrees_cycle ) )
@( numerics.arcsin( numerics.sin( angle_radians, radians_cycle ), radians_cycle ) );
new_line;
put( "Arccos" )
@( numerics.arccos( numerics.cos( angle_degrees, degrees_cycle ), degrees_cycle ) )
@( numerics.arccos( numerics.cos( angle_radians, radians_cycle ), radians_cycle ) );
new_line;
put( "Arctan" )
@( numerics.arctan( numerics.tan( angle_degrees, degrees_cycle ), 1, degrees_cycle ) )
@( numerics.arctan( numerics.tan( angle_radians, radians_cycle ), 1, radians_cycle ) );
new_line;
put( "Arccot" )
@( numerics.arccot( numerics.cot( angle_degrees, degrees_cycle ), 1, degrees_cycle ) )
@( numerics.arccot( numerics.cot( angle_radians, radians_cycle ), 1, radians_cycle ) );
new_line;
command_line.set_exit_status( 0 );
end trig;</syntaxhighlight>
{{out}}
<pre>
$ spar trig
Sin 7.07106781186547E-01 7.07106781186547E-01
Cos 7.07106781186547E-01 7.07106781186548E-01
Tan 1.00000000000000E+00 9.99999999999998E-01
Cot 1.00000000000000E+00 1.00000000000000E+00
Arcsin 4.50000000000000E+01 7.85398163397448E-01
Arccos 4.50000000000000E+01 7.85398163397448E-01
Arctan 45 7.85398163397448E-01
Arccot 45 7.85398163397449E-01</pre>
=={{header|SQL PL}}==
Line 5,297 ⟶ 5,559:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var d = 30
|