METHODS OF COMPUTING SQUARE ROOTS

This article presents and explains several methods which can be used to calculate square roots.

Contents
Exponential identity
Rough estimation
Babylonian method
Example
Convergence
Bakhshali approximation
Example
Digit by digit calculation
Examples
Mul/Div-free versions for int/long
Duplex method for extracting a square root
Example 1, by discussion
Example 1, analysis and square root framework
Example 2
> Duplex deduction.
5
Example 3
Iterative methods for reciprocal square roots
Taylor series
Other methods
Continued fraction expansion
Example, square root of 114 as a continued fraction
Pell's equation
Approximations that depend on IEEE representation
Reciprocal of the square root
See also
Notes
External links

Exponential identity


Pocket calculators typically implement good routines to compute the exponential function and the natural logarithm, and then compute the square root of ''S'' using the identity
:sqrt{S} = e^{ rac{1}{2}ln S}.
The same identity is used when computing square roots with logarithm tables or slide rules.

Rough estimation


Many of the methods for calculating square roots require an initial seed value. If the initial value is too far from the actual square root, the calculation will be slowed down. It is therefore useful to have a rough estimate, which may be very inaccurate but easy to calculate. One way of obtaining such an estimate for sqrt{S} is calculating 3''D'', where ''D'' is the number of digits (to the left of the decimal point) in a positive real ''S''. If ''S'' < 1, ''D'' is the negative of the number of zeros to the immediate right of the decimal point.
A better method of rough estimation is this:
:If ''D'' is odd, ''D'' = 2''n'' + 1, then use sqrt{S} pprox 2 cdot 10^n.
:If ''D'' is even, ''D'' = 2''n'' + 2, then use sqrt{S} pprox 6 cdot 10^n.
When working in the binary numeral system (as computers do internally), an alternative method is to use 2^{leftlfloor D/2
ight
floor} (here ''D'' is the number of binary digits).

Babylonian method



Graph charting the use of the Babylonian method for approximating the square root of 100 (10) using start values
''x''0=50,
''x''0=1,
and ''x''0=-5. Note that using a negative start value yields the negative root.

A commonly used algorithm for approximating sqrt S (and perhaps the best) is known as the "Babylonian method"[1]
and can be derived from (but predates) Newton's method. This is a quadratically convergent algorithm, which means that the number of correct digits of the approximation roughly doubles with each iteration. It proceeds as follows:
#Start with an arbitrary positive start value ''x''0 (the closer to the root, the better).
#Let ''x''''n''+1 be the average of ''x''''n'' and ''S'' / ''x''''n'' (using the arithmetic mean to approximate the geometric mean).
#Repeat steps 2 and 3, until the desired accuracy is achieved.
It can also be represented as:
:x_0 pprox sqrt{S}.
:x_{n+1} = rac{1}{2} left(x_n + rac{S}{x_n}
ight),
:sqrt S = lim_{n o infty} x_n.
This algorithm works equally well in the p-adic numbers, but cannot be used to identify real square roots with p-adic square roots; it is easy, for example, to construct a sequence of rational numbers by this method which converges to +3 in the reals, but to -3 in the 2-adics.
Example

We'll calculate sqrt{S}, where ''S'' = 125348, to 6 significant figures. Here ''D'', the number of digits in ''S'', is 6. Then:
:x_0 = 3^D = 3^6 = 729.000,!
:x_1 = rac{1}{2} left(x_0 + rac{S}{x_0}
ight) = rac{1}{2} left(729.000 + rac{125348}{729.000}
ight) = 450.472
:x_2 = rac{1}{2} left(x_1 + rac{S}{x_1}
ight) = rac{1}{2} left(450.472 + rac{125348}{450.472}
ight) = 364.365
:x_3 = rac{1}{2} left(x_2 + rac{S}{x_2}
ight) = rac{1}{2} left(364.365 + rac{125348}{364.365}
ight) = 354.191
:x_4 = rac{1}{2} left(x_3 + rac{S}{x_3}
ight) = rac{1}{2} left(354.191 + rac{125348}{354.191}
ight) = 354.045
:x_5 = rac{1}{2} left(x_4 + rac{S}{x_4}
ight) = rac{1}{2} left(354.045 + rac{125348}{354.045}
ight) = 354.045
Therefore, sqrt{125348} pprox 354.045.
Convergence

We let the relative error in ''x''n be defined by
:epsilon_n = rac {x_n}{sqrt{S}} - 1
and thus
:x_n = sqrt {S} cdot (1 + epsilon_n).
Then one can show that
:epsilon_{n+1} = rac {epsilon_n^2}{2 (1 + epsilon_n)}
and thus that
:0 leq epsilon_{n+2} leq min ( rac {epsilon_{n+1}^2}{2}, rac {epsilon_{n+1}}{2})
and consequently that convergence is assured provided that ''x''0 and ''S'' are both positive.

Bakhshali approximation


This is a method for finding an approximation to a square root which was described in an ancient manuscript known as the Bakhshali Manuscript. It is equivalent to two iterations of the Babylonian method beginning with ''N''. The original presentation goes as follows: To calculate sqrt{S}, let ''N''2 be the nearest perfect square to ''S''. Then, calculate:
:d = S - N^2 ,!
:P = rac{d}{2N}
:A = N + P,!
:sqrt{S} pprox A - rac{P^2}{2A}
This can be also written as:
:sqrt{S} pprox rac{N^4+6N^2S+S^2}{4N^3+4NS}
:sqrt{N^2 + d} pprox N + rac{d}{2N} - rac{d^2}{8N^3 + 4Nd}
Example

We'll find sqrt{9.2345}.
:N=3,!
:d = 9.2345 - 3^2 = 0.2345,!
:P = rac{0.2345}{2 cdot 3} = 0.0391
:A = 3 + 0.0391 = 3.0391,!
:sqrt{9.2345} pprox 3.0391 - rac{0.0391^2}{2 imes 3.0391} pprox 3.0388

Digit by digit calculation


This is a method to find each digit of the square root in a sequence. It is much slower than the Babylonian method (if you have a calculator which can divide in one operation), but it has several advantages:

★ It can be easier for manual calculations.

★ Every digit of the root found is known to be correct, i.e. it will not have to be changed later.

★ If the square root has an expansion which terminates, the algorithm will terminate after the last digit is found. Thus, it can be used to check whether a given integer is a square number.
Napier's bones include an aid for the execution of this algorithm. The Shifting nth-root algorithm is a generalization of this method.
The algorithm works for any base, and naturally, the way it proceeds depends on the base chosen. We will describe the method for the decimal system. It goes as follows:
Write the original number in decimal form. The numbers are written similar to the long division algorithm, and, as in long division, the root will be written on the line above. Now separate the digits into pairs, starting from the decimal point and going both left and right. The decimal point of the root will be above the decimal point of the square. One digit of the root will appear above each pair of digits of the square.
Beginning with the left-most pair of digits, do the following procedure for each pair:
# Starting on the left, bring down the most significant (leftmost) pair of digits not yet used (if all the digits have been used, write "00") and write them to the right of the remainder from the previous step (on the first step, there will be no remainder). In other words, multiply the remainder by 100 and add the two digits. This will be the 'current value ''c'''.
# Find ''p'', ''y'' and ''x'', as follows:
#
★ Let '''p''' be the 'part of the root found so far', ignoring any decimal point. (For the first step, ''p'' = 0).
#
★ Let '''y''' equal (20 cdot p + x) cdot x.
#
★ Determine the greatest digit '''x''' such that (20 cdot p + x) cdot x (that is, ''y'') does not exceed ''c''.
#

★ Note: 20''p'' + ''x'' is simply twice ''p'', with the digit ''x'' appended to the right).
#

★ Note: You can find ''x'' by guessing what ''c''/(20·''p'') is and doing a trial calculation of ''y'', then adjusting ''x'' upward or downward as necessary.
#
★ Place the digit x as the next digit of the root, i.e above the two digits of the square which you just brought down. Thus the next ''p'' will be the old ''p'' times 10 plus ''x''.
# Subtract ''y'' from ''c'' to form a new remainder.
# If the remainder is zero and there are no more digits to bring down, then the algorithm has terminated. Otherwise go back to step 1 for another iteration.
Examples

'Find the square root of 152.2756.'
1 2. 3 4
/ 01 52.27 56
01 1
★ 1 <= 1 < 2
★ 2 x = 1
01 y = x
★ x = 1
★ 1 = 1
00 52 22
★ 2 <= 52 < 23
★ 3 x = 2
00 44 y = (20+x)
★ x = 22
★ 2 = 44
08 27 243
★ 3 <= 827 < 244
★ 4 x = 3
07 29 y = (240+x)
★ x = 243
★ 3 = 729
98 56 2464
★ 4 <= 9856 < 2465
★ 5 x = 4
98 56 y = (2460+x)
★ x = 2464
★ 4 = 9856
00 00 Algorithm terminates: Answer is 12.34
'Find the square root of 2.'
1. 4 1 4 2
/ 02.00 00 00 00
02 1
★ 1 <= 2 < 2
★ 2 x = 1
01 y = x
★ x = 1
★ 1 = 1
01 00 24
★ 4 <= 100 < 25
★ 5 x = 4
00 96 y = (20+x)
★ x = 24
★ 4 = 96
04 00 281
★ 1 <= 400 < 282
★ 2 x = 1
02 81 y = (280+x)
★ x = 281
★ 1 = 281
01 19 00 2824
★ 4 <= 11900 < 2825
★ 5 x = 4
01 12 96 y = (2820+x)
★ x = 2824
★ 4 = 11296
06 04 00 28282
★ 2 <= 60400 < 28283
★ 3 x = 2
The desired precision is achieved:
The square root of 2 is about 1.4142
Mul/Div-free versions for int/long

These digit-by-digit implementations avoid multiplication/division:
(sources in java)
public static int sqrt(int num) {
int curLog = 16;
int res = 0;
while (curLog > 0) {
int nextNum = num - (res << curLog);
curLog--;
if (nextNum > 0) {
final int resAdd = (1 << curLog);
nextNum -= (resAdd << curLog);
if (nextNum > 0) {
num = nextNum;
res |= resAdd;
}
else if (nextNum == 0) return res | resAdd;
}
}
return res;
}
public static long sqrt(long num) {
int curLog = 32;
long res = 0;
while (curLog > 0) {
long nextNum = num - (res << curLog);
curLog--;
if (nextNum > 0) {
final long resAdd = (1L << curLog);
nextNum -= (resAdd << curLog);
if (nextNum > 0) {
num = nextNum;
res |= resAdd;
}
else if (nextNum == 0) return res | resAdd;
}
}
return res;
}
This is faster:
public static int sqrt(int num) {
int op = num;
int res = 0;
int one = 1 << 14; // The second-to-top bit is set: 1L<<30 for long
// "one" starts at the highest power of four <= than the argument.
while (one > op) one >>= 2;
while (one != 0) {
if (op >= res + one) {
op = op - (res + one);
res = res + 2
★ one;
}
res >>= 1;
one >>= 2;
}
return res;
}
and can be optimised further if the compiler doesn't spot that (res + one) is used three times.
For more bizarre integer algorithms, see [1].

Duplex method for extracting a square root


The duplex method is a variant of the digit by digit method for calculating the square root of a whole or decimal number one digit at a time.[2] The 'duplex' is the square of the central digit plus double the cross-product of digits equidistant from the center. The duplex is computed from the quotient digits (square root digits) computed thus far, but after the initial digits. The duplex is subtracted from the dividend digit prior to the second subtraction for the product of the quotient digit times the divisor digit. For perfect squares the duplex and the dividend will get smaller and reach zero after a few steps. For non-perfect squares the decimal value of the square root can be calculated to any precision desired. However, as the decimal places proliferate, the duplex adjustment gets larger and longer to calculate. The duplex method follows the Vedic ideal for an algorithm, one-line, mental calculation. It is flexible in choosing the first digit group and the divisor. Small divisors are to be avoided by starting with a larger initial group.
In short, to calculate the 'duplex' of a number, double the product of each pair of equidistant digits plus the square of the center digit (of the digits to the right of the colon).
Number => Calculation = Duplex
574 ==> 2(5·4) + 72 = 89
406,739 ==> 2(4·9)+ 2(0·3)+ 2(6·7) = 72+0+84 = 156
123,456 ==> 2(1·6)+ 2(2·5)+ 2(3·4) = 12 +20 +24 = 56
88,900,777 ==> 2(56)+2(56)+2(63)+0+0 = 320+30 = 350
48329,03711 ==> 2(4·1)+2(8·1)+2(3·7)+2(2·3)+2(9·0)= 8+16+42+12+0 = 78
In a square root calculation the quotient digit set increases incrementally for each step.
Number => Calculation = Duplex:
1 ==> 12 = 1
14 ==>2(1·4) = 8
142 ==> 2(1·2) + 42 = 4 + 16 = 20
14,21 ==> 2(1·1) + 2(4·2) = 2 + 16 = 18
14213 ==> 6+8+4 = 18
142,135 ==> 10+24+4 = 38
1421356 ==> 12+40+12+1 = 65
1421,3562 ==> 4+48+20+6 = 78
142,135,623 ==> 6+16+24+10+9 = 65
142,1356,237 ==> 14+24+8+12+30 = 88
142,13562,373 ==> 6+56+12+4+36+25 = 139
Example 1, by discussion

Consider the perfect square 2809. 532 = 2809. Use the duplex method to find the square root of 2,809.

★ Set down the number in 'groups of two digits'.

★ We define a '''divisor''', a '''dividend''' and a '''quotient''' to find the '''root'''.

★ Given 2809. Consider the first group, 28.


★ Find the nearest perfect square below that group.


★ The root of that perfect square is the first digit of our 'root'.


★ Since 28 > 25 and 25 = 52, we take 5 as the first digit in the square root.


★ For the 'divisor' we take double this first digit (2 · 5), which is 10.

★ Next, we set up a division framework with a colon.


★ 28: 0 9 is the 'dividend' and 5: is the 'quotient'.


★ We put a colon to the right of 28 and 5 and keep the colons lined up vertically. The 'duplex' is calculated only on quotient digits to the right of the colon.

★ We calculate the 'remainder'. 28: minus 25: is 3:.


★ We append the remainder on the left of the next digit to get the new dividend.


★ Here, we append 3 to the next dividend digit 0, which makes the new dividend 30. The divisor 10 goes into 30 just 3 times. (No reserve needed here for subsequent deductions.)

★ We repeat the operation.


★ The zero remainder appended to 9. Nine is the next dividend.


★ Now we have a digit to the right of the colon so we deduct the duplex, 32 = 9.


★ Subtracting this duplex from the dividend 9, we get a zero remainder.


★ Ten into zero is zero. The next root digit is zero. The next duplex is 2(3·0) = 0.


★ The dividend is zero. We have an exact square root, 53.0.
Example 1, analysis and square root framework

Find the square root of 2809.
Set down the number in groups of two digits.
The number of groups gives the number of whole digits in the root.
Put a colon after the first group, 28, to separate it.
From the first group, 28, we obtain the divisor, 10, since
28>25=52 and by doubling this first root, 2x5=10.
Gross dividend: 28: 0 9. Using mental math:
Divisor: 10) 3 0 Square: 10) 28: 30 9
Duplex, Deduction: 25: xx 09 Square root: 5: 3. 0
Dividend: 30 00
Remainder: 3: 00 00
Square Root, Quotient: 5: 3. 0
Example 2

Find the square root of 2,080,180,881.
Solution by the duplex method: this ten-digit square has five digit-pairs, so it will have a five-digit square root. The first digit-pair is 20. Put the colon to the right. The nearest square below 20 is 16, whose root is 4, the first root digit. So, we use 2·4=8 for the divisor. Now we proceed with the duplex division, one digit column at a time. Prefix the remainder to the next dividend digit.
divisor; gross dividend: 8) 20: 8 0 1 8 0 8 8 1
read the dividend diagonally up: 4 8 7 11 10 10 0 8
minus the duplex: 16: xx 25 60 36 90 108 00 81
actual dividend: : 48 55 11 82 10 00 08 00
minus the product: : 40 48 00 72 00 00 0 00
remainder: 4: 8 7 11 10 10 0 8 00
quotient: 4: 5, 6 0 9. 0 0 0 0
'Duplex calculations:'
Quotient-digits

> Duplex deduction.
5

> 52= 25
5 and 6 ==> 2(5·6) = 60
5,6,0 ==> 2(5·0)+62 = 36
5,6,0,9 ==> 2(5·9)+2(6·0) = 90
5,6,0,9,0 ==> 2(5·0)+2(6·9)+ 0 = 108
5,6,0,9,0,0 ==> 2(5·0)+2(6·0)+2(0·9) = 0
5,6,0,9,0,0,0 ==> 2(5·0)+2(6·0)+2(0·0)+92 = 81
Hence the square root of 2,080,180,881 is exactly 45,609.
Example 3

Find the square root of two to ten places. Let us take 20,000 as the beginning group, using three digit-pairs at the start. The perfect square just below 20,000 is 141, since 1412 = 19881 < 20,000. So, the first root digits are 141 and the divisor doubled, 2 x 141 = 282. With a larger divisor the duplex will be relatively small. Hence, we can pick the multiple of the divisor without confusion.
Dividend: 2.0000 : 0 0 0 0 0 0 0 0
Diagonal;Divisor: 282) : 119 62 40 102 162 182 75 112
Minus duplex: : xxxx 16 16 12 28 53 74 59
Actual dividend: 20000 : 1190 604 384 1008 1592 1767 676 1061
Minus product: 19881 : 1128 564 282 846 1410 1692 564 846
Remainder: 119 : 62 40 102 162 182 75 112 215
Root quotient: 1.41 : 4 2 1 3 5 6 2 3
Ten multiples of 282: 282; 564; 846; 1128; 1410; 1692; 1974; 2256; 2538; 2820.

Iterative methods for reciprocal square roots


The following are iterative methods for finding the reciprocal square root of ''S'' which is rac{1}{sqrt{S}}. Once it has been found, we can find sqrt{S} by simple multiplication: sqrt{S} = rac{1}{sqrt{S}} cdot S. These iterations involve only multiplication, and not division. They are therefore faster than the Babylonian method. However, they are not stable. If the initial value is not close to the reciprocal square root, the iterations will diverge away from it rather than converge to it. It can therefore be advantageous to perform an iteration of the Babylonian method on a rough estimate before starting to apply these methods.

★ One method is found by applying Newton's method to the equation rac{1}{x^2} - S = 0. It converges quadratically:
:x_{n+1} = rac{x_n}{2} cdot (3 - S cdot x_n^2).

★ Another method converges cubically, but involves more operations per iteration:
:y_n = S cdot x_n^2, , !
:x_{n+1} = rac{x_n}{8} cdot (15 - y_n cdot (10 - 3 cdot y_n)).

Taylor series


If ''N'' is an approximation to sqrt{S}, a better approximation can be found by using the Taylor series of the square root function:
:sqrt{N^2+d} = sum_{n=0}^infty rac{(-1)^{n}(2n)!d^n}{(1-2n)n!^2 4^nN^{2n-1}} = N + rac{d}{2N} - rac{d^2}{8N^3} + rac{d^3}{16N^5} - rac{5d^4}{128N^7} + cdots
As an iterative method, the order of convergence is equal to the number of terms used. With 2 terms, it is identical to the Babylonian method; With 3 terms, each iteration takes almost as many operations as the Bakhshali approximation, but converges more slowly. Therefore, this is not a particularly efficient way of calculation.

Other methods


Finding sqrt{S} is the same as solving the equation x^2 - S = 0,!. Therefore, any general numerical root-finding algorithm can be used. Newton's method, for example, reduces in this case to the Babylonian method. Other methods are less efficient than the ones presented above.

Continued fraction expansion


Quadratic irrationals (numbers of the form rac{a+sqrt{b}}{c}, where ''a'', ''b'' and ''c'' are integers), and in particular, square roots of integers, have periodic continued fractions. Sometimes we may be interested not in finding the numerical value of a square root, but rather in its continued fraction expansion. The following iterative algorithm can be used for this purpose (''S'' is any natural number which is not a perfect square):
:m_0=0,!
:d_0=1,!
:a_0=leftlfloorsqrt{S}
ight
floor,!
:m_{n+1}=d_na_n-m_n,!
:d_{n+1}= rac{S-m_{n+1}^2}{d_n},!
:a_{n+1} =leftlfloor rac{sqrt{S}+m_{n+1}}{d_{n+1}}
ight
floor =leftlfloor rac{a_0+m_{n+1}}{d_{n+1}}
ight
floor!.
Notice that ''m''n, ''d''n, and ''a''n are always integers.
The algorithm terminates when this triplet is the same as one encountered before. The expansion will repeat from then on. The sequence [''a''0; ''a''1, ''a''2, ''a''3, …] is the continued fraction expansion:
:sqrt{S} = a_0 + cfrac{1}{a_1 + cfrac{1}{a_2 + cfrac{1}{a_3+,cdots}}}
Example, square root of 114 as a continued fraction

We begin with ''m''0=0; ''d''0=1; and ''a''0=10.

sqrt{114}= rac{sqrt{114}+0}{1}=10+ rac{sqrt{114}-10}{1}= 10+ rac{(sqrt{114}-10)(sqrt{114}+10)}{sqrt{114}+10}
=10+ rac{114-100}{sqrt{114}+10}=10+ rac{1}{ rac{sqrt{114}+10}{14}}.

Next, ''m''1=10; ''d''1=14; and ''a''1=1.

rac{sqrt{114}+10}{14}=1+ rac{sqrt{114}-4}{14}=1+ rac{114-16}{14(sqrt{114}+4)}
=1+ rac{1}{ rac{sqrt{114}+4}{7}}.

Next, ''m''2=4; ''d''2=7; and ''a''2=2.

rac{sqrt{114}+4}{7}=2+ rac{sqrt{114}-10}{7}=2+ rac{14}{7(sqrt{114}+10)}
=2+ rac{1}{ rac{sqrt{114}+10}{2}}.

rac{sqrt{114}+10}{2}=10+ rac{sqrt{114}-10}{2}=10+ rac{14}{2(sqrt{114}+10)}
=10+ rac{1}{ rac{sqrt{114}+10}{7}}.
rac{sqrt{114}+10}{7}=2+ rac{sqrt{114}-4}{7}=2+ rac{98}{7(sqrt{114}+4)}
=2+ rac{1}{ rac{sqrt{114}+4}{14}}.
rac{sqrt{114}+4}{14}=1+ rac{sqrt{114}-10}{14}=1+ rac{14}{14(sqrt{114}+10)}
=1+ rac{1}{ rac{sqrt{114}+10}{1}}.
rac{sqrt{114}+10}{1}=20+ rac{sqrt{114}-10}{1}=20+ rac{14}{sqrt{114}+10}
=20+ rac{1}{ rac{sqrt{114}+10}{14}}.
Now, loop back to the second equation above.
Consequently, the continued fraction for the square root of 114 is:
sqrt{114} = [10;1,2,10,2,1,20,1,2,10,2,1,20,1,2,10,2,1,20,...].

Pell's equation


Pell's equation and its variants yield a method for efficiently finding continued fraction convergents of square roots of integers. However, it can be complicated to execute, and usually not every convergent is generated. The ideas behind the method are as follows:

★ If (''p'', ''q'') is a solution (where ''p'' and ''q'' are integers) to the equation p^2 = S cdot q^2 pm 1!, then rac{p}{q} is a continued fraction convergent of sqrt{S}, and as such, is an excellent rational approximation to it.

★ If (''p''a, ''q''a) and (''p''b, ''q''b) are solutions, then so is:
:p = p_a p_b + S cdot q_a q_b,!
:q = p_a q_b + p_b q_a,!

★ More generally, if (''p''1, ''q''1) is a solution, then it is possible to generate a sequence of solutions (''p''''n'', ''q''''n'') satisfying:
:p_{m+n} = p_m p_n + S cdot q_m q_n,!
:q_{m+n} = p_m q_n + p_n q_m,!
The method is as follows:

★ Find natural numbers (0 excluded) ''p''1 and ''q''1 such that p_1^2 = S cdot q_1^2 pm 1. This is the hard part; It can be done either by guessing, or by using fairly sophisticated techniques.
:
★ To generate a long list of convergents, iterate:
::p_{n+1} = p_1 p_n + S cdot q_1 q_n,!
::q_{n+1} = p_1 q_n + p_n q_1,!
:
★ To find the larger convergents quickly, iterate:
::p_{2n} = p_n^2 + S cdot q_n^2,!
::q_{2n} = 2 p_n q_n,!

★ In either case, rac{p_n}{q_n} is a rational approximation satisfying
:left| rac{p_n}{q_n} - sqrt{S}
ight| < rac{1}{q_n^2 cdot sqrt{S}}.

Approximations that depend on IEEE representation


On computers, a very rapid Newton's method based approximation to the square root can be obtained for floating point numbers when computers use an IEEE (or sufficiently similar) representation.
float fastsqrt(float val) {
int tmp =
★ (int
★ )&val;
tmp -= 1<<23; /
★ Remove last bit to not let it go to mantissa
★ /
/
★ tmp is now an approximation to logbase2(val)
★ /
tmp = tmp >> 1; /
★ divide by 2
★ /
tmp += 1<<29; /
★ add 64 to exponent: (e+127)/2 =(e/2)+63,
★ /
/
★ that represents (e/2)-64 but we want e/2
★ /
return
★ (float
★ )&tmp;
}
In the above, the operations to remove last exponent bit and add the IEEE bias can be combined into a single operation. An additional adjustment can be made in the same operation to reduce the maximum relative error. So, the three operations, not including the cast, can be rewritten as:
tmp = (1<<29) + (tmp >> 1) - (1<<22) + m;
Where ''m'' is some ''magic number'' that corresponds to a calculation involving an initial guess used in the Newton's approximation.
Reciprocal of the square root

A variant of the above routine is included below, which can be used to compute the reciprocal of the square root, i.e. x^{-{1over2}} instead, was written by Greg Walsh, and implemented into the game Quake 3 by Gary Tarolli. As an approximation, it produced a relative error of less than 4%, and in computer graphics it is a very efficient way to normalize a vector.
float invSqrt(float x)
{
float xhalf = 0.5f
★ x;
int i =
★ (int
★ )&x;
i = 0x5f3759df - (i >> 1);
x =
★ (float
★ )&i;
x = x
★ (1.5f - xhalf
★ x
★ x);
return x;
}

See also



Alpha max plus beta min algorithm

Integer square root

N-th root algorithm

Shifting nth-root algorithm

Square root of 2

Notes


1. There is no direct evidence showing how the Babylonians computed square roots, although there are informed conjectures. (Square root of 2#Notes gives a summary and references.)
2. 'Vedic Mathematics: Sixteen Simple Mathematical Formulae from the Vedas', by Swami Sankaracarya (1880-1960), Motilal Banarsidass Indological Publishers and Booksellers, Varnasi, India, 1965; reprinted in Delhi, India, 1975, 1978. 367 pages.

External links



MATHPATH: Square roots via Mediants

Rational Mean: High.Order square-root methods



A geometric view of the square root algorithm

Origin of Quake3's Fast InvSqrt()

Origin of Quake3's Fast InvSqrt() - Part Two

This article provided by Wikipedia. To edit the contents of this article, click here for original source.

psst.. try this: add to faves