Trigonometric functions: Difference between revisions

Added Easylang
(→‎{{header|Kotlin}}: made the Kotlin example not use Java)
(Added Easylang)
 
(4 intermediate revisions by 4 users not shown)
Line 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,134 ⟶ 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 4,760 ⟶ 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,118 ⟶ 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,355 ⟶ 5,559:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var d = 30
1,983

edits