Trigonometric functions: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: changed comment regarding DIGITS default. -- ~~~~) |
(→{{header|REXX}}: removed superflous semicolons, removed an assignment, changed showdigs to show more precision. -- ~~~~) |
||
Line 1,633: | Line 1,633: | ||
<br>than is needed, then reduce the digits and re-normalize a number (either with the FORMAT BIF or simply dividing by 1). |
<br>than is needed, then reduce the digits and re-normalize a number (either with the FORMAT BIF or simply dividing by 1). |
||
<br>Most math or trig functions need only five extra digits, but ten extra digits is safer in case the argument is close |
<br>Most math or trig functions need only five extra digits, but ten extra digits is safer in case the argument is close |
||
<br>to an asymptotic point or a multiple of pi or somesuch. |
<br>to an asymptotic point or a multiple or fractional part of pi or somesuch. |
||
<lang rexx>/*REXX program demonstrates some common trig functions ( |
<lang rexx>/*REXX program demonstrates some common trig functions (30 digits shown)*/ |
||
showdigs= |
showdigs=30 /*show only 30 digits of number. */ |
||
numeric digits showdigs+10 /*DIGITS default is 9, but use */ |
numeric digits showdigs+10 /*DIGITS default is 9, but use */ |
||
/*extra digs to prevent rounding.*/ |
/*extra digs to prevent rounding.*/ |
||
say 'Using' showdigs 'decimal digits precision.'; say |
|||
do j=-180 to +180 by 15 /*let's just do a half-Monty. */ |
do j=-180 to +180 by 15 /*let's just do a half-Monty. */ |
||
Line 1,656: | Line 1,657: | ||
exit /*REXX ignores the label's case. */ |
exit /*REXX ignores the label's case. */ |
||
/*─────────────────────────────────────subroutines──────────────────────*/ |
/*─────────────────────────────────────subroutines──────────────────────*/ |
||
Asin: procedure; arg x; if x<-1 | x>1 then call AsinErr; s=x*x |
Asin: procedure; arg x; if x<-1 | x>1 then call AsinErr; s=x*x |
||
if abs(x)>=.7 then return sign(x)*Acos(sqrt(1-s)); z=x; o=x; p=z |
if abs(x)>=.7 then return sign(x)*Acos(sqrt(1-s)); z=x; o=x; p=z |
||
do j=2 by 2; o=o*s*(j-1)/j; z=z+o/(j+1); if z=p then leave; p=z; end |
do j=2 by 2; o=o*s*(j-1)/j; z=z+o/(j+1); if z=p then leave; p=z; end |
||
return z |
return z |
||
Line 1,664: | Line 1,665: | ||
return Asin(x/sqrt(1+x**2)) |
return Asin(x/sqrt(1+x**2)) |
||
cos: procedure; arg x; x=r2r(x); a=abs(x); numeric fuzz min(9,digits()-9) |
cos: procedure; arg x; x=r2r(x); a=abs(x); numeric fuzz min(9,digits()-9) |
||
if a=pi() then return -1; if a=pi()/2 | a=2*pi() then return 0 |
if a=pi() then return -1; if a=pi()/2 | a=2*pi() then return 0 |
||
if a=pi()/3 then return .5; if a=2*pi()/3 then return -.5 |
if a=pi()/3 then return .5; if a=2*pi()/3 then return -.5 |
||
return sincos(1,1,-1) |
return sincos(1,1,-1) |
||
sin: procedure; arg x; x=r2r(x); numeric fuzz min(5,digits()-3) |
sin: procedure; arg x; x=r2r(x); numeric fuzz min(5,digits()-3) |
||
if abs(x)=pi() then return 0; return sinCos(x,x,1) |
if abs(x)=pi() then return 0; return sinCos(x,x,1) |
||
sinCos: parse arg z,_,i; x=x*x |
sinCos: parse arg z 1 p,_,i; x=x*x |
||
do k=2 by 2; _=-_*x/(k*(k+i));z=z+_; if z=p then leave;p=z;end; return z |
do k=2 by 2; _=-_*x/(k*(k+i));z=z+_; if z=p then leave;p=z;end; return z |
||
sqrt: procedure; parse arg x; if x=0 then return 0;d=digits();numeric digits 11 |
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits();numeric digits 11 |
||
g=sqrtGuess(); do j=0 while p>9; m.j=p; p=p%2+1; end |
g=sqrtGuess(); do j=0 while p>9; m.j=p; p=p%2+1; end |
||
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k; g=.5*(g+x/g); end |
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k; g=.5*(g+x/g); end |
||
numeric digits d; return g/1 |
numeric digits d; return g/1 |
||
sqrtGuess: if x<0 then call sqrtErr; numeric form scientific; m.=11; p=d+d%4+2 |
sqrtGuess: if x<0 then call sqrtErr; numeric form scientific; m.=11; p=d+d%4+2 |
||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; return g*.5'E'_%2 |
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; return g*.5'E'_%2 |
||
Line 1,688: | Line 1,689: | ||
/*matches the current NUMERIC DIGITS, up to 1 million digits.*/ |
/*matches the current NUMERIC DIGITS, up to 1 million digits.*/ |
||
exp: procedure; arg x; ix=x%1; if abs(x-ix)>.5 then ix=ix+sign(x); x=x-ix |
exp: procedure; arg x; ix=x%1; if abs(x-ix)>.5 then ix=ix+sign(x); x=x-ix |
||
z=1; _=1; w=z; do j=1; _=_*x/j; z=(z+_)/1; if z==w then leave; w=z; end |
z=1; _=1; w=z; do j=1; _=_*x/j; z=(z+_)/1; if z==w then leave; w=z; end |
||
if z\==0 then z=z*e()**ix; return z |
if z\==0 then z=z*e()**ix; return z |
||
Line 1,744: | Line 1,745: | ||
'''output''' |
'''output''' |
||
<pre style="overflow:scroll"> |
<pre style="overflow:scroll"> |
||
Using 30 decimal digits precision. |
|||
⚫ | |||
-165 degrees, rads=-2.8797932 sin=-0.2588190 cos=-0.9659258 tan= 0.2679491 |
|||
-150 degrees, rads=-2.6179938 sin=-0.5 cos=-0.8660254 tan= 0.5773502 |
|||
-135 degrees, rads=-2.3561944 sin=-0.7071067 cos=-0.7071067 tan= 1 |
|||
-120 degrees, rads=-2.0943951 sin=-0.8660254 cos=-0.5 tan= 1.7320508 |
|||
-105 degrees, rads=-1.8325957 sin=-0.9659258 cos=-0.2588190 tan= 3.7320508 |
|||
⚫ | |||
-75 degrees, rads=-1.3089969 sin=-0.9659258 cos= 0.2588190 tan=-3.7320508 |
|||
-60 degrees, rads=-1.0471975 sin=-0.8660254 cos= 0.5 tan=-1.7320508 |
|||
-45 degrees, rads=-0.7853981 sin=-0.7071067 cos= 0.7071067 tan=-1 |
|||
-30 degrees, rads=-0.5235987 sin=-0.5 cos= 0.8660254 tan=-0.5773502 |
|||
-15 degrees, rads=-0.2617993 sin=-0.2588190 cos= 0.9659258 tan=-0.2679491 |
|||
⚫ | |||
15 degrees, rads= 0.2617993 sin= 0.2588190 cos= 0.9659258 tan= 0.2679491 |
|||
30 degrees, rads= 0.5235987 sin= 0.5 cos= 0.8660254 tan= 0.5773502 |
|||
45 degrees, rads= 0.7853981 sin= 0.7071067 cos= 0.7071067 tan= 1 |
|||
60 degrees, rads= 1.0471975 sin= 0.8660254 cos= 0.5 tan= 1.7320508 |
|||
75 degrees, rads= 1.3089969 sin= 0.9659258 cos= 0.2588190 tan= 3.7320508 |
|||
90 degrees, rads= 1.5707963 sin= 1 cos= 0 |
|||
105 degrees, rads= 1.8325957 sin= 0.9659258 cos=-0.2588190 tan=-3.7320508 |
|||
120 degrees, rads= 2.0943951 sin= 0.8660254 cos=-0.5 tan=-1.7320508 |
|||
135 degrees, rads= 2.3561944 sin= 0.7071067 cos=-0.7071067 tan=-1 |
|||
150 degrees, rads= 2.6179938 sin= 0.5 cos=-0.8660254 tan=-0.5773502 |
|||
165 degrees, rads= 2.8797932 sin= 0.2588190 cos=-0.9659258 tan=-0.2679491 |
|||
⚫ | |||
-180 degrees, rads=-3.141592653589793238462643383 sin= 0 cos=-1 tan= 0 |
|||
-1 radians, degs=-57.295779 Acos= 3.1415926 Asin=-1.5707963 Atan=-0.7853981 |
|||
-165 degrees, rads=-2.879793265790643801924089768 sin=-0.258819045102520762348898837 cos=-0.965925826289068286749743199 tan= 0.267949192431122706472553658 |
|||
-0.5 radians, degs=-28.647889 Acos= 2.0943951 Asin=-0.5235987 Atan=-0.4636476 |
|||
-150 degrees, rads=-2.617993877991494365385536152 sin=-0.5 cos=-0.866025403784438646763723170 tan= 0.577350269189625764509148780 |
|||
⚫ | |||
-135 degrees, rads=-2.356194490192344928846982537 sin=-0.707106781186547524400844362 cos=-0.707106781186547524400844362 tan= 1 |
|||
0.5 radians, degs= 28.647889 Acos= 1.0471975 Asin= 0.5235987 Atan= 0.4636476 |
|||
-120 degrees, rads=-2.094395102393195492308428922 sin=-0.866025403784438646763723170 cos=-0.5 tan= 1.732050807568877293527446341 |
|||
1.0 radians, degs= 57.295779 Acos= 0 Asin= 1.5707963 Atan= 0.7853981 |
|||
-105 degrees, rads=-1.832595714594046055769875306 sin=-0.965925826289068286749743199 cos=-0.258819045102520762348898837 tan= 3.732050807568877293527446341 |
|||
⚫ | |||
-75 degrees, rads=-1.308996938995747182692768076 sin=-0.965925826289068286749743199 cos= 0.258819045102520762348898837 tan=-3.732050807568877293527446341 |
|||
-60 degrees, rads=-1.047197551196597746154214461 sin=-0.866025403784438646763723170 cos= 0.5 tan=-1.732050807568877293527446341 |
|||
-45 degrees, rads=-0.785398163397448309615660845 sin=-0.707106781186547524400844362 cos= 0.707106781186547524400844362 tan=-1 |
|||
-30 degrees, rads=-0.523598775598298873077107230 sin=-0.5 cos= 0.866025403784438646763723170 tan=-0.577350269189625764509148780 |
|||
-15 degrees, rads=-0.261799387799149436538553615 sin=-0.258819045102520762348898837 cos= 0.965925826289068286749743199 tan=-0.267949192431122706472553658 |
|||
⚫ | |||
15 degrees, rads= 0.261799387799149436538553615 sin= 0.258819045102520762348898837 cos= 0.965925826289068286749743199 tan= 0.267949192431122706472553658 |
|||
30 degrees, rads= 0.523598775598298873077107230 sin= 0.5 cos= 0.866025403784438646763723170 tan= 0.577350269189625764509148780 |
|||
45 degrees, rads= 0.785398163397448309615660845 sin= 0.707106781186547524400844362 cos= 0.707106781186547524400844362 tan= 1 |
|||
60 degrees, rads= 1.047197551196597746154214461 sin= 0.866025403784438646763723170 cos= 0.5 tan= 1.732050807568877293527446341 |
|||
75 degrees, rads= 1.308996938995747182692768076 sin= 0.965925826289068286749743199 cos= 0.258819045102520762348898837 tan= 3.732050807568877293527446341 |
|||
⚫ | |||
105 degrees, rads= 1.832595714594046055769875306 sin= 0.965925826289068286749743199 cos=-0.258819045102520762348898837 tan=-3.732050807568877293527446341 |
|||
120 degrees, rads= 2.094395102393195492308428922 sin= 0.866025403784438646763723170 cos=-0.5 tan=-1.732050807568877293527446341 |
|||
135 degrees, rads= 2.356194490192344928846982537 sin= 0.707106781186547524400844362 cos=-0.707106781186547524400844362 tan=-1 |
|||
150 degrees, rads= 2.617993877991494365385536152 sin= 0.5 cos=-0.866025403784438646763723170 tan=-0.577350269189625764509148780 |
|||
165 degrees, rads= 2.879793265790643801924089768 sin= 0.258819045102520762348898837 cos=-0.965925826289068286749743199 tan=-0.267949192431122706472553658 |
|||
⚫ | |||
-1 radians, degs=-57.29577951308232087679815481 Acos= 3.141592653589793238462643383 Asin=-1.570796326794896619231321691 Atan=-0.785398163397448309615660845 |
|||
-0.5 radians, degs=-28.64788975654116043839907740 Acos= 2.094395102393195492308428922 Asin=-0.523598775598298873077107230 Atan=-0.463647609000806116214256231 |
|||
⚫ | |||
0.5 radians, degs= 28.64788975654116043839907740 Acos= 1.047197551196597746154214461 Asin= 0.523598775598298873077107230 Atan= 0.463647609000806116214256231 |
|||
1.0 radians, degs= 57.29577951308232087679815481 Acos= 0 Asin= 1.570796326794896619231321691 Atan= 0.785398163397448309615660845 |
|||
</pre> |
</pre> |
||