Anonymous user
Roots of unity: Difference between revisions
m
→{{header|REXX}}: added whitespace, aligned statements, changed some comments, optimized the R2R function.
(add FreeBASIC) |
m (→{{header|REXX}}: added whitespace, aligned statements, changed some comments, optimized the R2R function.) |
||
Line 1,529:
(See the value of the REXX variable '''frac''', 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 /*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.*/
if frac='' | frac=="," then frac= 5 /* " " " " " " */
if n<0 then start= 1 /*Negative? Then use a range of K's. */
▲ say right(# 'roots of unity', 40, "─") ' (showing' frac "fractional decimal digits)"
do angle=0 by pi2/# for # /*compute the angle for each root. */
rp=
if left(rp, 1) \== '-' then rp= " "rp
ip=
if left(ip, 1) \== '-' then ip= "+"ip
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*/
end /*#*/
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
pi: pi=3.141592653589793238462643383279502884197169399375105820974944592307816; return pi
r2r: return arg(1) // ( pi()
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
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; z= 1; _= 1;
do k=2 by 2 until p=z; p=z; _= -
/*──────────────────────────────────────────────────────────────────────────────────────*/
sin: procedure; parse arg x; x= r2r(x); numeric fuzz min(5, digits()-3)
if abs(x)=pi then return 0; z= x; _= x;
do k=2 by 2 until p=z; p=z; _= -
{{out|output|text= when using the input of: <tt> 5 </tt>}}
<pre>
Line 1,574 ⟶ 1,573:
0.30902 -0.95106i
</pre>
{{out|output|text= when using the input of: <tt> 10 36 </tt>}}
<pre>
───────────────────────10 roots of unity (showing 36 fractional decimal digits)
Line 1,588 ⟶ 1,587:
0.809016994374947424102293417182819059 -0.587785252292473129168705954639072769i
</pre>
{{out|output|text= when using the input of: <tt> -12 </tt>}}
(Shown at five-sixths size.)
|