Anonymous user
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=
if frac='' | frac=="," then frac=
start= abs(n)
if n<0 then start=
numeric digits length( pi() ) - 1 /*use number of decimal digits in pi. */
pi2= pi +
/*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(
if left(rp, 1) \== '-' then rp=" "rp /*not negative? Then pad with a blank.*/
ip= adjust( sin(angle
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 )
/*──────────────────────────────────────────────────────────────────────────────────────*/
adjust: parse arg x; near0= '1e-' || (digits() - digits() % 10)
if abs(x) < near0 then x=
return format(x, , frac) /
/*──────────────────────────────────────────────────────────────────────────────────────*/
cos: procedure; parse arg x; x= r2r(x); a= abs(x);
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
/*──────────────────────────────────────────────────────────────────────────────────────*/
sin: procedure; parse arg x; x= r2r(x); numeric fuzz min(5, digits()
if abs(x)=pi then return 0;
do k=2 by 2 until p=z; p=z; _= - _*$x / (k*(k+1)); z= z+ _; end; return z</lang>
{{out|output|text= when using the input of: <tt> 5 </tt>}}▼
▲ do k=2 by 2 until p=z; p=z; _=-_*$x/(k*(k+i)); z=z+_; end; return z</lang>
▲{{out|output|text= when using the input of: <tt> 5 </tt>}}
<pre>
────────────────────────5 roots of unity (showing 5 fractional decimal digits)
|