CORDIC: Difference between revisions
→{{header|J}}: redo
(→{{header|J}}: redo) |
(→{{header|J}}: redo) |
||
Line 119:
cordic=: {{alpha=. y
XY=. 1 0 assert. 0 <: alpha assert. 0.
while. epsilon < alpha do.
k=. phin I. alpha
Line 128:
}}
'octant angle'=. 8 0.25p1#:y
select. octant
case. 0 do. cordic angle
case. 1 do. |.cordic 0.25p1-angle
case. 2 do. _1 1*|.cordic angle
case. 3 do. _1 1* cordic 0.25p1-angle
case. 4 do. _1 _1* cordic angle
case. 5 do. _1 _1*|.cordic 0.25p1-angle
case. 6 do. 1 _1*|.cordic angle
case. 7 do. 1 _1* cordic 0.25p1-angle
end.
}}</syntaxhighlight>
Task examples (cos is the left value in the result, argument is in radians):▼
_0.92954 _0.420445
CORDIC 0
1 0
CORDIC 1.5
0.
CORDIC 6
0.
▲Task examples (cos is the left value in the result, argument is in radians):
▲<syntaxhighlight lang=J>FIXME</syntaxhighlight>
=== Notes ===
CAUTION: At the time of this writing, the task description declares that the cordic algorithm is valid in the range 0 .. π/2. But it appears that the algorithm can only be valid in the range 0..π/4.
Here, we use three constants, two of which are lookup tables (<code>tent</code> gives us negative powers of ten as a lookup table):▼
▲
<syntaxhighlight lang=J> epsilon
Line 156 ⟶ 170:
<syntaxhighlight lang=J> {&phin
{ &0.785398163397448279 0.0996686524911620381 0.00999966668666523936 0.000999999666666867007 9.99999996666667089e_5 9.99999999966667302e_6 9.99999999999667283e_7 9.9999999999999744e_8 1.00000000000000085e_8 1.00000000000000089e_9 1.00000000000000107e_10 1....</syntaxhighlight>
That said, it's not clear that this algorithm can be more accurate than something near 2% for the general case.
Also: [[j:Vocabulary/SpecialCombinations#Preexecuting_verbs_with_.28.28_.29.29|double parenthesis around a noun phrase]] tells the interpreter that that expression is a constant which should be evaluated once, ahead of time.
|