Guest User

Untitled

a guest
Mar 17th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1.        Num sq_exp(Num sq) //up > down
  2.         {
  3.             int i0, i1 = IDS.sqr_exp_bits;
  4.             if (sq.down.IsEven && (up.Sign < 0)) IDS.sys.error("exp: neg sqr");
  5.             Num res = new Num(1), _sq = new Num(sq), tmp = new Num(1), _t = new Num(this); // (5/11-1/2)*2 = ((5*2-11)/(11*2))*2
  6.             BigInteger _ud = up / down;
  7.             if (_ud > 3)
  8.             {
  9.                 int l2 = Num._l2(_ud);
  10.                 while (l2 > 1)
  11.                 {
  12.                     _t._sq2_n(tmp, IDS.sqr2[l2]); i0 = IDS.sqr_steps; while (i0-- > 0) _t._sq2_n(tmp, tmp);
  13.                     _sq.up <<= 1; if (_sq.up >= _sq.down)
  14.                     {
  15.                         _sq.up -= _sq.down;
  16.                         res.mul(tmp);
  17.                     }
  18.                     if (_sq.up.IsZero || (i1 < 0)) return res.simple();
  19.                     _t.up = tmp.up; _t.down = tmp.down; l2 >>= 1; i1--;
  20.                 }
  21.             }
  22.             while ((!_sq.up.IsZero) && (i1 > 0))
  23.             {
  24.                 tmp.up = _t.up + _t.down; if (tmp.up.IsEven) { tmp.up >>= 1; tmp.down = BigInteger.Abs(_t.down); } else tmp.down = _t.down << 1;
  25.                 i0 = IDS.sqr_steps; while (i0-- > 0) _t._sq2_n(tmp, tmp);
  26.                 _sq.up <<= 1; if (_sq.up >= _sq.down)
  27.                 {
  28.                     _sq.up -= _sq.down;
  29.                     res.mul(tmp); res.prec_this();
  30.                 }
  31.                 _t.up = tmp.up; _t.down = tmp.down; i1--;
  32.             }
  33.             return res.simple();
  34.         }
Advertisement
Add Comment
Please, Sign In to add comment