Angle difference between two bearings: Difference between revisions

Content added Content deleted
(Scala contribution added.)
Line 1,236: Line 1,236:
161.50295230740448
161.50295230740448
-37.29885558826936</pre>
-37.29885558826936</pre>

=={{header|Pascal}}==

This program is meant to be saved in the same folder as a file <code>angles.txt</code> containing the input. Each pair of angles to subtract appears on its own line in the input file.

<lang Pascal>
Program Bearings;
{ Reads pairs of angles from a file and subtracts them }

Const
fileName = 'angles.txt';

Type
degrees = real;

Var
subtrahend, minuend: degrees;
angleFile: text;

function Simplify(angle: degrees): degrees;
{ Returns an number in the range [-180.0, 180.0] }
begin
while angle > 180.0 do
angle := angle - 360.0;
while angle < -180.0 do
angle := angle + 360.0;
Simplify := angle
end;

function Difference(b1, b2: degrees): degrees;
{ Subtracts b1 from b2 and returns a simplified result }
begin
Difference := Simplify(b2 - b1)
end;

procedure Subtract(b1, b2: degrees);
{ Subtracts b1 from b2 and shows the whole equation onscreen }
var
b3: degrees;
begin
b3 := Difference(b1, b2);
writeln(b2:20:11, ' - ', b1:20:11, ' = ', b3:20:11)
end;

Begin
assign(angleFile, fileName);
reset(angleFile);
while not eof(angleFile) do
begin
readln(angleFile, subtrahend, minuend);
Subtract(subtrahend, minuend)
end;
close(angleFile)
End.
</lang>

{{in}}

20 45
-45 45
-85 90
-95 90
-45 125
-45 145
29.4803 -88.6381
-78.3251 -159.036
-70099.74233810938 29840.67437876723
-165313.6666297357 33693.9894517456
1174.8380510598456 -154146.66490124757
60175.77306795546 42213.07192354373

{{out}}
45.00000000000 - 20.00000000000 = 25.00000000000
45.00000000000 - -45.00000000000 = 90.00000000000
90.00000000000 - -85.00000000000 = 175.00000000000
90.00000000000 - -95.00000000000 = -175.00000000000
125.00000000000 - -45.00000000000 = 170.00000000000
145.00000000000 - -45.00000000000 = -170.00000000000
-88.63810000000 - 29.48030000000 = -118.11840000000
-159.03600000000 - -78.32510000000 = -80.71090000000
29840.67437876723 - -70099.74233810938 = -139.58328312339
33693.98945174560 - -165313.66662973570 = -72.34391851869
-154146.66490124760 - 1174.83805105985 = -161.50295230740
42213.07192354373 - 60175.77306795546 = 37.29885558827


=={{header|Perl 6}}==
=={{header|Perl 6}}==