sqrt.txt
来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· 文本 代码 · 共 83 行
TXT
83 行
Square RootA simple iterative algorithm is used to compute the greatest integerless than or equal to the square root. Essentially, this is Newton'slinear approximation, computed by finding successive values of theequation: x[k]^2 - Vx[k+1] = x[k] - ------------ 2 x[k]...where V is the value for which the square root is being sought. Inessence, what is happening here is that we guess a value for thesquare root, then figure out how far off we were by squaring our guessand subtracting the target. Using this value, we compute a linearapproximation for the error, and adjust the "guess". We keep doingthis until the precision gets low enough that the above equationyields a quotient of zero. At this point, our last guess is onegreater than the square root we're seeking.The initial guess is computed by dividing V by 4, which is a heuristicI have found to be fairly good on average. This also has theadvantage of being very easy to compute efficiently, even for largevalues.So, the resulting algorithm works as follows: x = V / 4 /* compute initial guess */ loop t = (x * x) - V /* Compute absolute error */ u = 2 * x /* Adjust by tangent slope */ t = t / u /* Loop is done if error is zero */ if(t == 0) break /* Adjust guess by error term */ x = x - t end x = x - 1The result of the computation is the value of x.------------------------------------------------------------------The contents of this file are subject to the Mozilla PublicLicense Version 1.1 (the "License"); you may not use this fileexcept in compliance with the License. You may obtain a copy ofthe License at http://www.mozilla.org/MPL/Software distributed under the License is distributed on an "ASIS" basis, WITHOUT WARRANTY OF ANY KIND, either express orimplied. See the License for the specific language governingrights and limitations under the License.The Original Code is the MPI Arbitrary Precision Integer Arithmeticlibrary.The Initial Developer of the Original Code is Michael J. Fromberger <sting@linguist.dartmouth.edu>Portions created by Michael J. Fromberger are Copyright (C) 1998, 2000 Michael J. Fromberger. All Rights Reserved.Contributor(s):Alternatively, the contents of this file may be used under theterms of the GNU General Public License Version 2 or later (the"GPL"), in which case the provisions of the GPL are applicableinstead of those above. If you wish to allow use of yourversion of this file only under the terms of the GPL and not toallow others to use your version of this file under the MPL,indicate your decision by deleting the provisions above andreplace them with the notice and other provisions required bythe GPL. If you do not delete the provisions above, a recipientmay use your version of this file under either the MPL or the GPL.$Id: sqrt.txt,v 1.1 2000/07/14 00:44:37 nelsonb%netscape.com Exp $
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?