Trigonometric functions: Difference between revisions

→‎{{header|Fortran}}: Some fortrans are fatter than others.
(→‎{{header|Fortran}}: Some fortrans are fatter than others.)
Line 805:
COSH(x) ! Hyperbolic cosine
TANH(x) ! Hyperbolic tangent</lang>
 
But, for those with access to fatter Fortran function libraries, trigonometrical functions working in degrees are also available.
<lang Fortran>
Calculate various trigonometric functions from the Fortran library.
INTEGER BIT(32),B,IP !Stuff for bit fiddling.
INTEGER ENUFF,I !Step through the test angles.
PARAMETER (ENUFF = 17) !A selection of special values.
INTEGER ANGLE(ENUFF) !All in whole degrees.
DATA ANGLE/0,30,45,60,90,120,135,150,180, !Here they are.
1 210,225,240,270,300,315,330,360/ !Thus check angle folding.
REAL PI,DEG2RAD !Special numbers.
REAL D,R,FD,FR,AD,AR !Degree, Radian, F(D), F(R), inverses.
PI = 4*ATAN(1.0) !SINGLE PRECISION 1.0.
DEG2RAD = PI/180 !Limited precision here too for a transcendal number.
Case the first: sines.
WRITE (6,10) ("Sin", I = 1,4) !Supply some names.
10 FORMAT (" Deg.",A7,"(Deg)",A7,"(Rad) Rad - Deg", !Ah, layout.
1 6X,"Arc",A3,"D",6X,"Arc",A3,"R",9X,"Diff")
DO I = 1,ENUFF !Step through the test values.
D = ANGLE(I) !The angle in degrees, in floating point.
R = D*DEG2RAD !Approximation, in radians.
FD = SIND(D); AD = ASIND(FD) !Functions working in degrees.
FR = SIN(R); AR = ASIN(FR)/DEG2RAD !Functions working in radians.
WRITE (6,11) INT(D),FD,FR,FR - FD,AD,AR,AR - AD !Results.
11 FORMAT (I4,":",3F12.8,3F13.7) !Ah, alignment with FORMAT 10...
END DO !On to the next test value.
Case the second: cosines.
WRITE (6,10) ("Cos", I = 1,4)
DO I = 1,ENUFF
D = ANGLE(I)
R = D*DEG2RAD
FD = COSD(D); AD = ACOSD(FD)
FR = COS(R); AR = ACOS(FR)/DEG2RAD
WRITE (6,11) INT(D),FD,FR,FR - FD,AD,AR,AR - AD
END DO
Case the third: tangents.
WRITE (6,10) ("Tan", I = 1,4)
DO I = 1,ENUFF
D = ANGLE(I)
R = D*DEG2RAD
FD = TAND(D); AD = ATAND(FD)
FR = TAN(R); AR = ATAN(FR)/DEG2RAD
WRITE (6,11) INT(D),FD,FR,FR - FD,AD,AR,AR - AD
END DO
WRITE (6,*) "...Special deal for 90 degrees..."
D = 90
R = D*DEG2RAD
FD = TAND(D); AD = ATAND(FD)
FR = TAN(R); AR = ATAN(FR)/DEG2RAD
WRITE (6,*) "TanD =",FD,"Atan =",AD
WRITE (6,*) "TanR =",FR,"Atan =",AR
Convert PI to binary...
PI = PI - 3 !I know it starts with three, and I need the fractional part.
BIT(1:2) = 1 !So, the binary is 11. something.
B = 2 !Two bits known.
DO I = 1,26 !For single precision, more than enough additional bits.
PI = PI*2 !Hoist a bit to the hot spot.
IP = PI !The integral part.
PI = PI - IP !Remove it from the work in progress.
B = B + 1 !Another bit bitten.
BIT(B) = IP !Place it.
END DO !On to the next.
WRITE (6,20) BIT(1:B) !Reveal the bits.
20 FORMAT (" Pi ~ ",2I1,".",66I1) !A known format.
WRITE (6,*) " = 11.00100100001111110110101010001000100001..." !But actually...
END !So much for that.
</lang>
Output:
Deg. Sin(Deg) Sin(Rad) Rad - Deg ArcSinD ArcSinR Diff
0: 0.00000000 0.00000000 0.00000000 0.0000000 0.0000000 0.0000000
30: 0.50000000 0.50000000 0.00000000 30.0000000 30.0000000 0.0000000
45: 0.70710677 0.70710677 0.00000000 45.0000000 45.0000000 0.0000000
60: 0.86602539 0.86602545 0.00000006 60.0000000 60.0000038 0.0000038
90: 1.00000000 1.00000000 0.00000000 90.0000000 90.0000000 0.0000000
120: 0.86602539 0.86602539 0.00000000 60.0000000 60.0000000 0.0000000
135: 0.70710677 0.70710677 0.00000000 45.0000000 45.0000000 0.0000000
150: 0.50000000 0.50000006 0.00000006 30.0000000 30.0000038 0.0000038
180: 0.00000000 -0.00000009 -0.00000009 0.0000000 -0.0000050 -0.0000050
210: -0.50000000 -0.49999997 0.00000003 -30.0000000 -29.9999981 0.0000019
225: -0.70710677 -0.70710671 0.00000006 -45.0000000 -44.9999962 0.0000038
240: -0.86602539 -0.86602545 -0.00000006 -60.0000000 -60.0000038 -0.0000038
270: -1.00000000 -1.00000000 0.00000000 -90.0000000 -90.0000000 0.0000000
300: -0.86602539 -0.86602545 -0.00000006 -60.0000000 -60.0000038 -0.0000038
315: -0.70710677 -0.70710689 -0.00000012 -45.0000000 -45.0000076 -0.0000076
330: -0.50000000 -0.50000018 -0.00000018 -30.0000000 -30.0000114 -0.0000114
360: 0.00000000 0.00000017 0.00000017 0.0000000 0.0000100 0.0000100
Deg. Cos(Deg) Cos(Rad) Rad - Deg ArcCosD ArcCosR Diff
0: 1.00000000 1.00000000 0.00000000 0.0000000 0.0000000 0.0000000
30: 0.86602539 0.86602539 0.00000000 30.0000019 30.0000019 0.0000000
45: 0.70710677 0.70710677 0.00000000 45.0000000 45.0000000 0.0000000
60: 0.50000000 0.49999997 -0.00000003 60.0000000 60.0000038 0.0000038
90: 0.00000000 -0.00000004 -0.00000004 90.0000000 90.0000000 0.0000000
120: -0.50000000 -0.50000006 -0.00000006 120.0000000 120.0000076 0.0000076
135: -0.70710677 -0.70710677 0.00000000 135.0000000 135.0000000 0.0000000
150: -0.86602539 -0.86602539 0.00000000 150.0000000 150.0000000 0.0000000
180: -1.00000000 -1.00000000 0.00000000 180.0000000 180.0000000 0.0000000
210: -0.86602539 -0.86602539 0.00000000 150.0000000 150.0000000 0.0000000
225: -0.70710677 -0.70710683 -0.00000006 135.0000000 135.0000000 0.0000000
240: -0.50000000 -0.49999991 0.00000009 120.0000000 119.9999924 -0.0000076
270: 0.00000000 0.00000001 0.00000001 90.0000000 90.0000000 0.0000000
300: 0.50000000 0.49999991 -0.00000009 60.0000000 60.0000076 0.0000076
315: 0.70710677 0.70710665 -0.00000012 45.0000000 45.0000114 0.0000114
330: 0.86602539 0.86602533 -0.00000006 30.0000019 30.0000095 0.0000076
360: 1.00000000 1.00000000 0.00000000 0.0000000 0.0000000 0.0000000
Deg. Tan(Deg) Tan(Rad) Rad - Deg ArcTanD ArcTanR Diff
0: 0.00000000 0.00000000 0.00000000 0.0000000 0.0000000 0.0000000
30: 0.57735026 0.57735026 0.00000000 30.0000000 30.0000000 0.0000000
45: 1.00000000 1.00000000 0.00000000 45.0000000 45.0000000 0.0000000
60: 1.73205078 1.73205090 0.00000012 60.0000000 60.0000000 0.0000000
90:************************************ 90.0000000 -90.0000000 -180.0000000
120: -1.73205078 -1.73205054 0.00000024 -60.0000000 -59.9999962 0.0000038
135: -1.00000000 -1.00000000 0.00000000 -45.0000000 -45.0000000 0.0000000
150: -0.57735026 -0.57735032 -0.00000006 -30.0000000 -30.0000019 -0.0000019
180: 0.00000000 0.00000009 0.00000009 0.0000000 0.0000050 0.0000050
210: 0.57735026 0.57735026 0.00000000 30.0000000 30.0000000 0.0000000
225: 1.00000000 0.99999988 -0.00000012 45.0000000 44.9999962 -0.0000038
240: 1.73205078 1.73205125 0.00000048 60.0000000 60.0000076 0.0000076
270:************************************ 90.0000000 -90.0000000 -180.0000000
300: -1.73205078 -1.73205113 -0.00000036 -60.0000000 -60.0000038 -0.0000038
315: -1.00000000 -1.00000024 -0.00000024 -45.0000000 -45.0000076 -0.0000076
330: -0.57735026 -0.57735056 -0.00000030 -30.0000000 -30.0000134 -0.0000134
360: 0.00000000 0.00000017 0.00000017 0.0000000 0.0000100 0.0000100
...Special deal for 90 degrees...
TanD = 1.6331778E+16 Atan = 90.00000
TanR = -2.2877332E+07 Atan = -90.00000
Pi ~ 11.00100100001111110110110000
= 11.00100100001111110110101010001000100001...
Notice that the calculations in radians are less accurate. Firstly, pi cannot be represented exactly and secondly, the conversion factor of pi/180 or 180/pi adds further to the error. The degree-based functions obviously can fold their angles using exact arithmetic (though ACosD has surprising trouble with 30°) and so 360° is the same as 0°, unlike the case with radians. TanD(90°) should yield Infinity (but, which sign?) but perhaps this latter-day feature of computer floating-point was not included. In any case, Tan(90° in radians) faces the problem that its parameter will not in fact be pi/2 but some value just over (or under), and likewise with double precision and quadruple precision and any other finite precision.
 
=={{header|F_Sharp|F#}}==
1,220

edits