Roots of unity: Difference between revisions

m
→‎{{header|REXX}}: added whitespace, simplified code, optimized two functions.
m (→‎{{header|REXX}}: added whitespace, simplified code, optimized two functions.)
Line 1,405:
<lang rexx>/*REXX program computes the K roots of unity (which usually includes complex roots).*/
parse arg n frac . /*get optional arguments from the C.L. */
if n=='' | n=="," then n=1 1 /*Not specified? Then use the default.*/
if frac='' | frac=="," then frac=5 5 /* " " " " " " */
start= abs(n) /*assume only one K is wanted. */
if n<0 then start=1 1 /*Negative? Then use a range of K's. */
numeric digits length( pi() ) - 1 /*use number of decimal digits in pi. */
pi2= pi +pi pi /*obtain the value of pi doubled. */
/*display unity roots for a range, or */
do #=start to abs(n) /* just for one K. */
say right(# 'roots of unity', 40, "─") ' (showing' frac "fractional decimal digits)"
do angle=0 by pi2/# for # /*compute the angle for each root. */
rp= adjust( cos( angle ) ) /*compute real part via COS function.*/
if left(rp, 1) \== '-' then rp=" "rp /*not negative? Then pad with a blank.*/
ip= adjust( sin(angle)) ) /*compute imaginary part via SIN funct.*/
if left(ip, 1) \== '-' then ip="+"ip /*Not negative? Then pad with + char.*/
if ip=0 then say rp /*Only real part? Ignore imaginary part*/
Line 1,426:
/*──────────────────────────────────────────────────────────────────────────────────────*/
pi: pi=3.141592653589793238462643383279502884197169399375105820974944592307816; return pi
r2r: return arg(1) // ( pi() * 2 ) /*reduce #radians: -2pi──► +2pi radians*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
adjust: parse arg x; near0= '1e-' || (digits() - digits() % 10) /*compute a tiny number.*/
if abs(x) < near0 then x=0 0 /*if it's near zero, then assume zero.*/
return format(x, , frac) / 1 1 /*fraction digits past decimal point. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cos: procedure; parse arg x; x= r2r(x); a= abs(x); numeric fuzz min(9, digits()-9)
if a=pi/3 then return .5; if a=pi/2|a=pi*2 then return 0
if a=pi then return -1; if a=pi*2/3 then return -.5; return .sincos(z=1,; _=1,-1); $x= x * x
do k=2 by 2 until p=z; p=z; _= - _*$x / (k*(k+i-1)); z= z+ _; end; return z</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
sin: procedure; parse arg x; x= r2r(x); numeric fuzz min(5, digits() - 3)
if abs(x)=pi then return 0; return .sincos( z= x,; _=x,; 1) $x= x * x
do k=2 by 2 until p=z; p=z; _= - _*$x / (k*(k+1)); z= z+ _; end; return z</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 5 </tt>}}
.sincos: parse arg z,_,i; $x= x * x
do k=2 by 2 until p=z; p=z; _=-_*$x/(k*(k+i)); z=z+_; end; return z</lang>
{{out|output|text=&nbsp; when using the input of: &nbsp; <tt> 5 </tt>}}
<pre>
────────────────────────5 roots of unity (showing 5 fractional decimal digits)