[q-lang-users] improvement to Q
Brought to you by:
agraef
From: <ed...@ri...> - 2007-06-27 19:24:10
|
<p><br />Albert Graef:</p><p>I hope other Q users don't boo me but, I would= like to see x^n return integer results when x is an integer and n is a nat= ural number. Maybe even return a rational when n is a negative integer. In = the meantime</p><p>public (^^) X N @ (^);<br />X^^0 =3D 1;<br />X^^1 =3D X = if X 0;<br /> =3D _FAIL_ otherwise;<br />X^^N:Int = <br /> if N>1:<br /> =3D W*W where W =3D X= ^^(N div 2) if N mod 2 =3D 0;<br /> =3D X*(X^^(N-1)= ) if N mod 2 =3D 1;<br /> otherwise:<br /> = =3D 1%(X^^(-N));</p><p>Example:</p><p> =3D=3D> 2^^3<br />8<br />=3D=3D&g= t; 2^^(-3)<br />1%8<br />=3D=3D> -2^^2<br />-4<br />=3D=3D> (-2)^^(-2= )<br />1%4 </p><p>Eddie<br /> =0D </p><BR>= |