# Can exact square roots not be found?

I'm brushing up on some higher level maths for a programming project. I was going along and then I realized that I have absolutely no idea how square roots can be computed.

I mean sure, I've memorized a lot of perfect squares. But I wouldn't be able to get an exact answer from some arbitrary number like 432,434, would I?

I Googled how to calculate square roots and everything always points to it basically being based on algorithms which have a degree of error because they're more or less guesses.

I can't seem to find out why it's impossible to get an exact square root though. Like why can't you plug in $$x$$ to a function $$f(x)$$ and get the exact square root?

• What do you expect to be the exact "computed" value of $\sqrt{2}$, say? – metamorphy Dec 20 '18 at 10:38
• Depending on how many bits you use for your floats, exact values do not exist for irrational numbers to computers. Increasing, for example, from 4 bit to 8 bit floats increases the accuracy, but does not eliminate rounding errors, even for rational numbers. For example $1+2^{-53}-1=2^{-53}$ obviously, but MATLAB for instance will tell you this sum is "exactly" zero – User123456789 Dec 20 '18 at 10:52
• Forget about square roots; you can't even calculate 1/10 accurately using IEEE floats. If you are brushing up on your math for a project, you need to know the at least the bare minimum that all programmers must know to work with floating point numbers. If you have not read docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html do not do any more math programming until you have! – Eric Lippert Dec 20 '18 at 14:43
• @User123456789 exact binary values for computers don't exist even for many rational numbers. But one can represent many irrationals precisely if using another representation (instead of a single float). – Džuris Dec 20 '18 at 14:50
• Let's make things simpler. What do you expect to be the exact computed value of $\frac{1}{9}$ ? – Brian Dec 20 '18 at 15:25

The square roots of non-perfect-square-integers are irrational numbers, which means that they have an infinite number of decimals, that do not repeat. So it would be a little tedious to write down the exact value...

Square roots can be computed, among others, by the so-called Heron's method (known BC), which is of the "guess" type, but converges extremely fast.

• However, numbers such as $\sqrt{432434}$ can be written as a periodic continued fraction of this form: $$[a_0;a_1,a_2,\dots,a_k,\overline{a_{k+1},a_{k+2},\dots,a_{k+m}}]$$ – Jeppe Stig Nielsen Dec 21 '18 at 17:30
• @JeppeStigNielsen: it can also be written compactly as the positive solution of $z^2=432434$. – Yves Daoust Dec 21 '18 at 18:29

I understand that this question is actually about the representation of the square root in binary (float) or decimal notation.

However, that is not what these numbers are about themselves.

$$\sqrt 3$$ is itself a perfectly valid number and an exact representation of the number that is the square root of 3. It can't be exactly represented as a binary or decimal number as these representations ar fairly limited (you can't even find an exact $$1/3$$ in either of those). But you can find it up to a certain precision.

Furthermore, these representations are actually used in computers. For example, Wolfram Mathematica will not replace $$\sqrt 3$$ or $$\frac 2 {17}$$ with an approximate representation which could screw up simplification later on. If you need the precision, you can exactly the same - store the numbers in a fancy structure that allows you to save the fact that it's a root of some number.

• You can find the square root to a given precision if the number is small enough, or of certain very specific types. Not all numbers can be stored in a physical medium, let alone be manipulated. – Lysistrata Mar 8 at 12:58

It depends what data types you have available in your programming environment. Typically, you’d be using double-precision floating-point numbers. But floating-point numbers are always rational numbers (quotient of two integers), and square roots are often not rational, so they can’t be represented exactly by floating-point values.

If you have a more exotic set of data types, you may be able to represent some (but not all) square roots exactly.

But there’s not really much point calculating square roots exactly unless our divisions and trig functions are also exact (which they are not, in floating-point math).

• Comments are not for extended discussion; this conversation has been moved to chat. – Aloizio Macedo Dec 22 '18 at 14:54

You can't find the exact value of square roots for non-square number. For the same reason you can't calculate the exact value of $$\pi$$ or $$e$$.

It's not a question of programming or approximation. Square roots of non-square numbers are irrational numbers.

One of their properties is that you can't calculate an exact, numeric value for them, you can't represent them in decimal notation. You can represent them with a symbol or approximate them, but that's it.

$$\sqrt 2$$ is actually the most famous irrational number, and is the reason we discovered them.

• I wonder if, rather than saying you can't find the exact value, it would be more correct to say that you can't express the exact value in decimal notation (or similar notation in other number bases). If you allow other ways of representing values, e.g. a notation using the square root symbol, then the problem becomes trivial. – Michael Kay Dec 20 '18 at 15:14
• @MichaelKay It is still not trivial, because you need to show that there IS a "number" (however you choose to define of "number") that corresponds to the thing you are talking about. (Simple example: there is no REAL number which is the square root of $-1$). – alephzero Dec 20 '18 at 15:28
• How are you defining "square number" here? 6.25 isn't something I would think is usually regarded as a square number but it has a rational root – Martin Smith Dec 20 '18 at 16:33
• @MartinSmith Fair point. I don't know if I should defined more precisely to get just the ones that are irrational or say "most non-square numbers". – Teleporting Goat Dec 20 '18 at 16:38

Technically speaking, when you find $$\sqrt2 = 1.414 ...$$, you're calculating the decimal expansion of the square root of 2. When people say things like "calculate the square root of 2", what they generally mean is to find this decimal expansion. That's because when people think of the term "number", what they generally think of is "number represented in decimal format". Since the decimal expansion has an infinite number of digits, it's impossible to calculate in its entirety. (Although, more technically speaking, decimal expansion is writing the number in terms of powers of ten, while floating point expresses numbers in terms of powers of two, but that distinction doesn't affect this particular issue).

However, if we consider "number" as simply an abstract data class with particular methods, then it is possible to have a data class in which "square root of two" is a valid object, and an object whose square is exactly equal to two. In this context, the phrase "calculate the square root of two" would not have a clear meaning. We might have a print method that takes a number of digits, and outputs that number of digits. For instance, we might have that two.power(one.divide(two)).print(5) (i.e "print five digits after the decimal point of 2^(1/2)") gives the output 1.41421, but precise language would refer to this in terms of the method that is defined in the class, rather than just referring to it as "calculating" the square root of two. If you just type two.power(one.divide(two)), then depending on how the class is implemented, it may indeed "calculate" the floating point representation of $$\sqrt 2$$, but it may represent the object in a more abstract form. In the former case, two.power(one.divide(two)).power(two) (that is, $$(\sqrt 2)^2)$$) could be slightly less than 2, but in the latter case, it could very well return exactly 2. (It could also return exactly 2 in the former case, depending on how the rounding is set up).

An irrational number cannot be equated to a finite series of rational numbers, only an infinite series of such.

Assuming $$x$$ is rational, a fairly compact infinite series of rational numbers which equate to the square root and its reciprocal, that are derived from the generalized binomial theorem, are given below:

$$\sqrt{x} = \sum_{k=0}^\infty \frac{1}{4^k}\binom{2k}{k}\left(1-\frac{1}{x} \right)^k$$ and

$$\frac{1}{\sqrt{x}} = -\sum_{k=0}^\infty \frac{1}{4^k \left( 2k-1\right)}\binom{2k}{k}\left(1-\frac{1}{x} \right)^k$$

where $$x>1$$. See here for proof.

Unfortunately these series are much slower to converge to the desired values than other approximation/iterative methods available. Not even the most efficient algorithm will find you the exact value for a square root in your lifetime.

Square roots are typically computed by root finding; if you want to find $$\sqrt{x}$$, you would use a root finding algorithm on

$$g(t) = t^2 - x$$

One such algorithm would be, for example, Newton's Method. Wikipedia has an entire page describing algorithms for root finding, and some more for computing square roots.

The unifying theme behind these algorithms is that you define a sequence $$\{x_n\}_{n \in \mathbb{N}}$$ such that $$x_n \to x^*$$, where $$g(x^*) = 0$$ and, depending on the algorithm and function, you can prove that it converges to the true root, and that it does so with a certain speed. The convergence properties usually depend on a good first approximation to where the root is, so not everything's rose colored. However, a priori this means that you can get arbitrarily close to the root and, for well behaved inputs, you will get the actual root (i.e. perfect squares).

As many other answers described, computers work with a floating point representation, which are limited in what they can represent to a subset of the rational numbers. This means, in practice, that most numbers can't in fact be handled by your computer, and furthermore that you have to take several precautions when working with them; "What Every Computer Scientist Should Know About Floating-Point Arithmetic" is a pretty classic introduction, which is also friendly for math students.

Computer Algebra Systems usually choose to instead work with a symbolic representation of math: instead of using actual numbers, implement the typical rules of math on symbols. In this approach, you would have a representation for $$\sqrt{2}$$, and a rule that tells you $$\sqrt{x}^2 = x$$; thus $$\sqrt{2}^2 = 2$$. At the end, they do have to approximate, but they have the most accurate representation possible until the very last minute, which avoids many errors.

If you are interested in reading more, "Numerical Analysis" by Richard L. Burden is a book I really liked when taking my Numerical Analysis class.

Square roots(and all other roots and some other interesting functions) of some numbers are irrational numbers. It means that they have infinite trail of digits after a decimal point(or comma in some other languages) and these digits are not periodic. Your screen has a finite number of pixels so it cannot depict an infinite number of digits. Therefore you will have to omit some of them. Even if you depict 50000 first digits and omit the rest of infinity it's still not exactly precise.

However don't think that such numbers are somehow unreal. They are used to measure physical quantities in given units. For instance if you have a square with a side exactly 1 meter long then its diagonal equals exactly square root of 2.

• "It means that they have infinite trail of digits after a comma" or the decimal point if you're American. ;-) – Phil N DeBlanc Dec 20 '18 at 12:27

Depending on what you're trying to do with the square root once it's been computed, a pair of floating point numbers bracketing the true value might be good enough. This is called Interval arithmetic .

Here's an example demonstrating the use of pyinterval (credit here for extracting the bits of a double in Python).

from interval import interval, imath
import struct

# https://stackoverflow.com/a/23624284/931154
def double_to_hex(f):
return hex(struct.unpack('<Q', struct.pack('<d', f))[0])

a = interval(10)
b = imath.sqrt(a)

print(b)
print(double_to_hex(b[0][0]))
print(double_to_hex(b[0][1]))


When run, this script produces the following output, the two hexadecimal numbers are the bit patterns of the floats, so it's clear they aren't identical.

interval([3.16227766017, 3.16227766017])


Because isqrt (interval sqrt) has a dedicated implementation in the library, the interval produced is as narrow as possible, but functions do not produce narrow intervals in general. Successive operations tend to cause the interval to grow. It's also important to keep in mind that interval arithmetic is inherently pessimistic, especially if the same variable appears multiple times in the expression of interest.

Even supposing you could symbolically handle irrational numbers in your program, if x is a rational and f(x) can be irrational, there's no rigorously defined (i.e. finite) algorithm to implement f, whatever it may be. Square root is just one example of such f.

You cannot express (using rational numbers) the square root of an arbitrary integer x with a mathematical tool less complicated than an infinite sum.

That said, if you want a fancy, practical yet fascinating (that's opinionated) answer to your original problem, I'd suggest reading this: https://en.wikipedia.org/wiki/Fast_inverse_square_root

You cannot always get exact square roots because of the nature of the numbers, for example square root of 2 is irrational meaning you have a infinity of decimals

## protected by Aloizio Macedo♦Dec 22 '18 at 14:54

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).