高效率開平方演算法

來源:互聯網
上載者:User

標籤:style   blog   color   for   div   amp   log   演算法   

 1 UINT sqrt(long m) 2 { 3  4     BYTE i,j; 5     UINT z,temp0,temp1; 6     long x,y; 7     x = 1; 8  9     for(j =0;j<16;j++)10     {11         x <<= 2;12         if(m<x)13             break;14     }15     x = y =1;16     x <<=2*j;17     y = x<<1;18 19     z = 0;20     temp0 = 0;21     for(i=0;i<=j;i++)22     {23         z <<= 1;24         temp0 <<= 2;25         if(m&x)26             temp0++;27         if(m&y)28             temp0 += 0x02;29         m <<= 2;30         temp1  = z;31         temp1 = (temp1<<1) + 1;32         if(temp1<=temp0)33         {34             temp0 -=temp1; 35             z++;36         }37     }38 39     return z;    40 41 }

演算法原理不是很清楚,但確實管用,開平方的值只精確到個位,誤差最大為1。哪位大神明白的給指點下。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.