Square roots of positive numbers are not in general
rational numbers, and so cannot be written as a terminating or recurring decimal expression. Therefore in general any attempt to compute a square root expressed in decimal form can only yield an approximation, though a sequence of increasingly accurate approximations can be obtained. Most
pocket calculators have a square root key. Computer
spreadsheets and other
software are also frequently used to calculate square roots. Pocket calculators typically implement efficient routines, such as the
Newton's method (frequently with an initial guess of 1), to compute the square root of a positive real number. When computing square roots with
logarithm tables or
slide rules, one can exploit the identities\sqrt{a} = e^{(\ln a)/2} = 10^{(\log_{10} a)/2}, where and are the
natural and
base-10 logarithms. By trial-and-error, one can square an estimate for \sqrt{a} and raise or lower the estimate until it agrees to sufficient accuracy. For this technique it is prudent to use the identity(x + c)^2 = x^2 + 2xc + c^2, as it allows one to adjust the estimate by some amount and measure the square of the adjustment in terms of the original estimate and its square. The most common
iterative method of square root calculation by hand is known as the "
Babylonian method" or "Heron's method" after the first-century Greek philosopher
Heron of Alexandria, who first described it. The method uses the same iterative scheme as the
Newton–Raphson method yields when applied to the function , using the fact that its slope at any point is , but predates it by many centuries. The algorithm is to repeat a simple calculation that results in a number closer to the actual square root each time it is repeated with its result as the new input. The motivation is that if is an overestimate to the square root of a nonnegative real number then with a bare text in one paragraph: it is the ugliest pair among 3 existing styles --> will be an underestimate and so the average of these two numbers is a better approximation than either of them. However, the
inequality of arithmetic and geometric means shows this average is always an overestimate of the square root (as noted
below), and so it can serve as a new overestimate with which to repeat the process, which
converges as a consequence of the successive overestimates and underestimates being closer to each other after each iteration. To find : • Start with an arbitrary positive start value . The closer to the square root of , the fewer the iterations that will be needed to achieve the desired precision. • Replace by the average between and . • Repeat from step 2, using this average as the new value of . That is, if an arbitrary guess for \sqrt{a} is , and , then each is an approximation of \sqrt{a} which is better for large than for small . If is positive, the convergence is
quadratic, which means that in approaching the limit, the number of correct digits roughly doubles in each next iteration. If , the convergence is only linear; however, \sqrt{0} = 0 so in this case no iteration is needed. Using the identity\sqrt{a} = 2^{-n}\sqrt{4^n a}, the computation of the square root of a positive number can be reduced to that of a number in the range . This simplifies finding a start value for the iterative method that is close to the square root, for which a
polynomial or
piecewise-linear approximation can be used. The
time complexity for computing a square root with digits of precision is equivalent to that of multiplying two -digit numbers. Another useful method for calculating the square root is the shifting nth root algorithm, applied for . The name of the square root
function varies from
programming language to programming language, with sqrt (often pronounced "squirt") being common, used in
C and derived languages such as
C++,
JavaScript,
PHP, and
Python. ==Square roots of negative and complex numbers== The square of any positive or negative number is positive, and the square of 0 is 0. Therefore, no negative number can have a
real square root. However, it is possible to work with a more inclusive set of numbers, called the
complex numbers, that does contain solutions to the square root of a negative number. This is done by introducing a new number, denoted by (sometimes by , especially in the context of
electricity where
i traditionally represents electric current) and called the
imaginary unit, which is
defined such that . Using this notation, we can think of as the square root of −1, but we also have and so is also a square root of −1. By convention, the principal square root of −1 is , or more generally, if is any nonnegative number, then the principal square root of is \sqrt{-x} = i \sqrt x. The right side (as well as its negative) is indeed a square root of , since(i\sqrt x)^2 = i^2(\sqrt x)^2 = (-1)x = -x. For every non-zero complex number there exist precisely two numbers such that : the principal square root of (defined below), and its negative.
Principal square root of a complex number To find a definition for the square root that allows us to consistently choose a single value, called the
principal value, we start by observing that any complex number x + i y can be viewed as a point in the plane, (x, y), expressed using
Cartesian coordinates. The same point may be reinterpreted using
polar coordinates as the pair (r, \varphi), where r \geq 0 is the distance of the point from the origin, and \varphi is the angle that the line from the origin to the point makes with the positive real (x) axis. In complex analysis, the location of this point is conventionally written r e^{i\varphi}. Ifz = r e^{i \varphi} \text{ with } -\pi then the of z is defined to be the following:\sqrt{z} = \sqrt{r} e^{i \varphi / 2}. The principal square root function is thus defined using the non-positive real axis as a
branch cut. If z is a non-negative real number (which happens if and only if \varphi = 0) then the principal square root of z is \sqrt{r} e^{i (0) / 2} = \sqrt{r}; in other words, the principal square root of a non-negative real number is just the usual non-negative square root. It is important that -\pi because if, for example, z = - 2 i (so \varphi = -\pi/2) then the principal square root is\sqrt{-2 i} = \sqrt{2 e^{i\varphi}} = \sqrt{2} e^{i\varphi/2} = \sqrt{2} e^{i(-\pi/4)} = 1 - i but using \tilde{\varphi} := \varphi + 2 \pi = 3\pi/2 would instead produce the other square root \sqrt{2} e^{i\tilde{\varphi}/2} = \sqrt{2} e^{i(3\pi/4)} = -1 + i = - \sqrt{-2 i}. The principal square root function is
holomorphic everywhere except on the set of non-positive real numbers (on strictly negative reals it is not even
continuous). The above Taylor series for \sqrt{1 + x} remains valid for complex numbers x with |x| The above can also be expressed in terms of
trigonometric functions:\sqrt{r \left(\cos \varphi + i \sin \varphi \right)} = \sqrt{r} \left(\cos \frac{\varphi}{2} + i \sin \frac{\varphi}{2} \right).
Algebraic formula When the number is expressed using its real and imaginary parts, the following formula can be used for the principal square root: \sqrt{x+iy} = \sqrt{\tfrac12\bigl(\sqrt{\textstyle x^2+y^2} + x\bigr)} + i\sgn(y) \sqrt{\tfrac12\bigl(\sqrt{\textstyle x^2+y^2} - x\bigr)}, where if and otherwise. In particular, the imaginary parts of the original number and the principal value of its square root have the same sign. The real part of the principal value of the square root is always nonnegative. For example, the principal square roots of are given by: \sqrt{i} = \frac{1+i}{\sqrt2}, \qquad \sqrt{-i} = \frac{1 - i}{\sqrt2}.
Notes In the following, the complex and may be expressed as: • z = |z| e^{i \theta_z} • w = |w| e^{i \theta_w} where -\pi and -\pi. Because of the discontinuous nature of the square root function in the complex plane, the following laws are
not true in general. • \sqrt{zw} = \sqrt{z} \sqrt{w} Counterexample for the principal square root: and This equality is valid only when -\pi • \frac{\sqrt{w}}{\sqrt z} = \sqrt{\frac{w}{z}} Counterexample for the principal square root: and This equality is valid only when -\pi • \sqrt{z^*} = \left( \sqrt z \right)^* Counterexample for the principal square root: ) This equality is valid only when \theta_z\ne\pi A similar problem appears with other complex functions with branch cuts, e.g., the
complex logarithm and the relations or which are not true in general. Wrongly assuming one of these laws underlies several faulty "proofs", for instance the following one showing that : \begin{align} -1 &= i \cdot i \\ &= \sqrt{-1} \cdot \sqrt{-1} \\ &= \sqrt{\left(-1\right)\cdot\left(-1\right)} \\ &= \sqrt{1} \\ &= 1. \end{align} The third equality cannot be justified (see
invalid proof). It can be made to hold by changing the meaning of √ so that this no longer represents the principal square root (see above) but selects a branch for the square root that contains \sqrt{1}\cdot\sqrt{-1}. The left-hand side becomes either\sqrt{-1} \cdot \sqrt{-1}=i \cdot i=-1 if the branch includes or\sqrt{-1} \cdot \sqrt{-1}=(-i) \cdot (-i)=-1 if the branch includes , while the right-hand side becomes\sqrt{\left(-1\right)\cdot\left(-1\right)} = \sqrt{1} = -1, where the last equality, \sqrt{1} = -1, is a consequence of the choice of branch in the redefinition of . ==th roots and polynomial roots==