Back to Main Page Source codeProgram Trigs; { Gerson Washiski Barbosa - Dec/2007 } { Trigonometric and inverse trigonometric functions (17-digit accuracy) } { To be compiled in TurboBCD (Turbo Pascal v3.02) } { Running times: Sin, Cos: 160.5 us;  Tan: 321.0 us @ 500 MHz } { All angles in DEGREES } var    x: Real;      txt: Text; function Sqrt(x: Real): real; var sq, t: Real; begin   if x<>0 then     begin       sq:=x/2;       repeat         t:=sq;         sq:=(sq+x/sq)/2       until sq=t;       Sqrt:=sq     end     else       Sqrt:=0 end; function Intg(x: Real): Real; var r: Real; begin   r:=Int(x);   if x<0 then r:=r-1;   Intg:=r end; function Mdl(x, y: Real): Real; begin   Mdl:=x-y*Intg(x/y) end; function Sign(x: Real): Integer; begin   if x<>0 then       Sign:=Trunc((x)/Abs(x))     else       Sign:=0 end; function Sin(x: Real): Real; const A =  5.81776417331443192E-03;       B = -3.28183761370851117E-08;       C =  5.55391614470475312E-14;       D = -4.47571324262354212E-20;       E =  2.10398182046943194E-26;       F = -6.47383342002944734E-33;       G =  1.40457976326925111E-39;       H = -2.25584840859272218E-46; var sn,x2: Real; begin   x:=Mdl(x,360);   if x>90     then       if x<270         then           x:=180-x         else           x:=x-360;   x2:=x*x;   sn:=x*(A+x2*(B+x2*(C+x2*(D+x2*(E+x2*(F+x2*(G+H*x2)))))));   Sin:=sn*(3-4*sn*sn) end; function Cos(x: Real): Real; begin   Cos:=Sin(90-x) end; function Tan(x: Real): Real; begin   if Mdl(x,180)<>90     then        Tan:=Sin(x)/Cos(x)     else        Tan:=Sign(x)*9.99999999999999999E+49 end; function ArcTan(x: real): Real; const PI_2 =  1.57079632679489662E+00;    { pi/2 }       PI_6 =  0.52359877559829887E+00;    { pi/6 }       SQR3 =  1.73205080756887729E+00;    { sqrt(3) }       TLS3 =  2.67949192431122706E-01;    { 2 - sqrt(3) }        R2D =  5.72957795130823209E+01;    { 180/pi }          A =  1.00000000000000000E+00;          B = -3.33333333333333320E-01;          C =  1.99999999999969080E-01;          D = -1.42857142845613967E-01;          E =  1.11111109453593870E-01;          F = -9.09089712757808854E-02;          G =  7.69182100185790154E-02;          H = -6.65496746226271243E-02;          I =  5.71652168403095978E-02;          J = -3.95856057285009705E-02; var at, k1, k2, x2: real;             s1, s2: integer; begin   s1:=Sign(x);   x:=Abs(x);   if x<1 then       begin         s2:=1;         k1:=0       end     else       begin         x:=1/x;         s2:=-1;         k1:=PI_2       end;   if x>TLS3     then       begin         x:=(x*SQR3-1)/(x+SQR3);         k2:=PI_6       end     else       k2:=0;   x2:=x*x;   at:=x*(A+x2*(B+x2*(C+x2*(D+x2*(E+x2*(F+x2*(G+x2*(H+x2*(I+J*x2)))))))));   ArcTan:=(k1+s2*(k2+at))*s1*R2D end; function ArcSin(x: real): real; begin   if Abs(x)<=1     then       if Abs(x)<1         then           ArcSin:=ArcTan(x/(Sqrt(1-x*x)))         else           ArcSin:=Sign(x)*90     else       begin         Write(^G);         WriteLn('ArcSin Error')       end; end; function ArcCos(x: real): real; begin   if Abs(x)<=1     then       if Abs(x)<1         then           ArcCos:=2*ArcTan(Sqrt((1-x)/(1+x)))         else           ArcCos:=90*(1-x)      else        begin          Write(^G);          WriteLn('ArcCos Error')        end; end; begin   ClrScr;   Assign(txt,'OUTPUT.TXT');   Rewrite(txt);   x:=-180;   repeat   if Trunc(x) Mod 60 = 0 then     begin       WriteLn(txt);       WriteLn(txt,'  x',' ':11,'Sin(x)',' ':18,'Cos(x)',' ':18,'Tan(x)');       WriteLn(txt)     end;     WriteLn(txt,x:4:0,'  ',Sin(x):21:18,'   ',Cos(x):21:18,'  ',Tan(x));     x:=x+1   until x>179;   WriteLn(txt);   x:=-1;   WriteLn(txt,'   x',' ':9,'ArcSin(x)',' ':16,'ArcCos(x)',' ':16,'ArcTan(x)');   WriteLn(txt);   repeat     WriteLn(txt,x:6:1,'  ',ArcSin(x):22:18,'  ',ArcCos(x):22:18,'  ',ArcTan(x):22:18);     x:=x+0.1   until x>1;   x:=-150;   repeat     if x<>0 then WriteLn(txt,x:6:1,'        ------------            ------------      ',ArcTan(x):22:18);     x:=x+10   until x>150;   WriteLn(txt);   WriteLn(txt,' ArcSin(ArcCos(ArcTan(Tan(Cos(Sin(9)))))) = ',ArcSin(ArcCos(ArcTan(Tan(Cos(Sin(9)))))):20:18);   Close(txt) end. Sample outputx           Sin(x)                  Cos(x)                  Tan(x) -151  -0.484809620246337029   -0.874619707139395801    5.54309051452768917E-01 -150  -0.500000000000000001   -0.866025403784438647    5.77350269189625766E-01 -149  -0.515038074910054210   -0.857167300702112288    6.00860619027560414E-01   59   0.857167300702112288    0.515038074910054210    1.66427948235051791E+00   60   0.866025403784438647    0.500000000000000001    1.73205080756887729E+00   61   0.874619707139395801    0.484809620246337029    1.80404775527142394E+00   89   0.999847695156391238    0.017452406437283513    5.72899616307594247E+01   90   1.000000000000000000    0.000000000000000000    9.99999999999999999E+49   91   0.999847695156391238   -0.017452406437283513   -5.72899616307594247E+01 134   0.719339800338651138   -0.694658370458997285   -1.03553031379056951E+00 135   0.707106781186547523   -0.707106781186547523   -1.00000000000000000E+00 136   0.694658370458997285   -0.719339800338651138   -9.65688774807074045E-01    x         ArcSin(x)                ArcCos(x)                ArcTan(x)   -1.0  -90.000000000000000000 180.000000000000000000 -45.000000000000000300   -0.9  -64.158067236832871500 154.158067236832871000 -41.987212495816659900   -0.8  -53.130102354155979000 143.130102354155979000 -38.659808254090090300    0.0    0.000000000000000000   90.000000000000000600    0.000000000000000000    0.1    5.739170477266786350   84.260829522733213200    5.710593137499642520    0.2   11.536959032815487700   78.463040967184512000   11.309932474020213100   0.4   23.578178478201830900 66.421821521798168400   21.801409486351811500    0.5   29.999999999999999800 59.999999999999999600   26.565051177077989100   0.6   36.869897645844021100   53.130102354155978200   30.963756532073521100-150.0          ----------            ------------      -89.618033795270974600-140.0          ----------            ------------      -89.590751391965061500  10.0          ----------            ------------       84.289406862500357400  20.0          ----------            ------------       87.137594773888252600  ArcSin(ArcCos(ArcTan(Tan(Cos(Sin(9)))))) = 8.999999999996491040 Back to Programs Page