Anonymous user
Trigonometric functions: Difference between revisions
→{{header|REXX}}: removed superflous semicolons, removed an assignment, changed showdigs to show more precision. -- ~~~~
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:
<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>to an asymptotic point or a multiple or fractional part of pi or somesuch.
<lang rexx>/*REXX program demonstrates some common trig functions (
showdigs=
numeric digits showdigs+10 /*DIGITS default is 9, but use */
/*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. */
Line 1,656 ⟶ 1,657:
exit /*REXX ignores the label's case. */
/*─────────────────────────────────────subroutines──────────────────────*/
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
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
Line 1,664 ⟶ 1,665:
return Asin(x/sqrt(1+x**2))
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()/3 then return .5; if a=2*pi()/3 then return -.5
return sincos(1,1,-1)
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)
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
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
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
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
Line 1,688 ⟶ 1,689:
/*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
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
Line 1,744 ⟶ 1,745:
'''output'''
<pre style="overflow:scroll">
Using 30 decimal digits precision.
-180 degrees, rads=-3.1415926 sin= 0 cos=-1 tan= 0▼
-90 degrees, rads=-1.5707963 sin=-1 cos= 0▼
0 degrees, rads= 0 sin= 0 cos= 1 tan= 0▼
180 degrees, rads= 3.1415926 sin= 0 cos=-1 tan= 0▼
-180 degrees, rads=-3.141592653589793238462643383 sin= 0 cos=-1 tan= 0
-165 degrees, rads=-2.879793265790643801924089768 sin=-0.258819045102520762348898837 cos=-0.965925826289068286749743199 tan= 0.267949192431122706472553658
-150 degrees, rads=-2.617993877991494365385536152 sin=-0.5 cos=-0.866025403784438646763723170 tan= 0.577350269189625764509148780
0 radians, degs= 0 Acos= 1.5707963 Asin= 0 Atan= 0▼
-135 degrees, rads=-2.356194490192344928846982537 sin=-0.707106781186547524400844362 cos=-0.707106781186547524400844362 tan= 1
-120 degrees, rads=-2.094395102393195492308428922 sin=-0.866025403784438646763723170 cos=-0.5 tan= 1.732050807568877293527446341
-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
▲ 0 degrees, rads= 0 sin= 0 cos= 1 tan= 0
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>
|