Roots of unity: Difference between revisions

m
→‎{{header|REXX}}: simplified some code, used a more idiomatic way for computing numeric digits (precision), added/changed whitespace and comments.
(Added Wren)
m (→‎{{header|REXX}}: simplified some code, used a more idiomatic way for computing numeric digits (precision), added/changed whitespace and comments.)
Line 1,595:
(See the value of the REXX variable &nbsp; '''frac''', &nbsp; 4<sup>th</sup> line).
<lang rexx>/*REXX program computes the K roots of unity (which usually includes complex roots).*/
numeric digits length( pi() ) - 1; d= digitslength(.) /*use number of decimal digits in pi. */
parse arg n frac . /*get optional arguments from the C.L. */
if n=='' | n=="," then n= 1 /*Not specified? Then use the default.*/
Line 1,601:
start= abs(n) /*assume only one K is wanted. */
if n<0 then start= 1 /*Negative? Then use a range of K's. */
do #=start to abs(n) /*show unity roots (for a range or 1 K).*/
say right(# 'roots of unity', 40, "─") ' (showing' frac "fractional decimal digits)"
do angle=0 by pi*2/# for # /*compute the angle for each root. */
Rp= adj( cos(angle) ) /*computethe real part via COS function.*/
Ip= adj( sin(angle) ) /*compute " imaginary part via " " SIN funct. " */
if Rp>=0 then Rp= ' 'Rp if Rp>=0 then Rp= " "Rp /*Not neg? Then pad with a blank char.*/
if Ip>=0 then Ip= '+'Ip if Ip>=0 then Ip= "+"Ip /* " " " " " " plus " */
if Ip =0 then say Rp /*Only real part? Ignore imaginary part*/
else say left(Rp, frac+4)Ip'i' /*display the real and imaginary part. */
end /*angle*/
Line 1,614:
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
adj: parse arg x; if abs(x) < ('1e-')(ddigits()*9%10) then x= 0; return format(x, , frac) / 1
pi: pi=3.141592653589793238462643383279502884197169399375105820974944592307816; return pi
r2r: pi2= pi() + pi; return arg(1) // pi2 /*reduce #radians: -2pi ─► +2pi radians*/