Comments

You must log in or register to comment.

mfb- t1_j240p1l wrote

It's a combination of tables with a few entries and clever algorithms.

What calculators do is a bit more complicated, but here is an approach that can be done with pen and paper and some time. Let's say we want to find the square root of 40. We know (look up) that 36 is a square, namely 6^(2), so the square root of 40 will be somewhere close to that.

40/6 = 6.666...

6 is too small for the square root, but 6.666... must be too large, so let's take the average of the two, 6.3333:

40/6.3333 = 6.3161 - pretty close!

Let's take the average of 6.3333 and 6.3161, which is 6.3247:

40/6.3247 = 6.32441 - even closer. Doing that two more steps:

6.324555321991

6.324555320336758664214273

Digits in bold are correct, so we got 18 correct digits in just four steps (or 19 if we round).

More digits of the square root of 40 for comparison: 6.324555320336758663997787

153

squigs t1_j24ngkj wrote

Really surprising how quickly this converges! I'd have thought we'd get 2 binary digits per step, not 4ish decimal!

9

mfb- t1_j24rwja wrote

It's one of the methods that converge really quickly, you double the number of correct digits with each step (that statement is independent of the base, and applies once you start getting some correct digits).

7

FearlessFaa t1_j248t9v wrote

I state here two mathematical facts used in the answer:

  • for any positive numbers m and n, where m < n and m, n > 0, the following is true:
    • m < (m + n) / 2 < n,
      • where (m + n) / 2 is the average of numbers m and n
  • for any numbers x > 1, it is true that:
    • x < x * x = x^2,
      • where x^2 is the square of x
7

mfb- t1_j24bs0w wrote

x < x * x is only true for x>1, but that's not relevant here.

What we do need (for x>0 and y>0): if x < y then y/x > 1 or equivalently y^2 / x > y. Same for the other direction, if x > y then y^2 / x < y. Here y is the square root we are looking for. This inequality makes sure one of our numbers is always too small while the other one is too large, i.e. the square root is in between.

3

Psyese t1_j24evge wrote

So how much memory does the calculator need to store all the squares in a table?

1

mfb- t1_j24l46t wrote

As mentioned, a calculator will typically do it a bit differently. Usually numbers are stored in floating point format. 40 wouldn't be stored as "40", but as 1.01*2^5 where 1.01 is a binary number. 1.01*2^5 = 101000 in binary which is 32+8=40 in decimal.

The square root of a product of two numbers is the product of square roots: sqrt(a*b) = sqrt(a)*sqrt(b). To calculate the square root of 2^(n), we just need to divide n by 2: sqrt(2^(5)) = 2^(5/2) = 2^2 * sqrt(2). The calculator knows sqrt(2), so it only needs to calculate the square root of 1.01 (in binary), or more generally any number between 1.0 and 1.111... = 10 (i.e. 2 in decimal). The smallest and largest number only differ by a factor 2. You can make a few entries in a table to use as starting values, or just use the same value for all cases - it won't be that far off anyway. Do a few steps of the method I showed, multiply the result by the square root of 2 if needed (if the original exponent was odd), combine it with the integer powers of 2 (here 2^(2)) and you get the square root.

15

RSA0 t1_j24rosr wrote

You don't need a table of squares. This method works, even if you don't start with a square. You just need to start as close as possible to the square root.

Even some crude approximation, like "closest power of 10", is enough for the initial value.

3

thefancyyeller t1_j27tfm3 wrote

Since it's binary and every stay is "squared" can't they just shift it?

1

mfb- t1_j28frma wrote

> and every stay is "squared"

Huh?

Bit shifts multiply by powers of 2.

1

Chromotron t1_j242mzs wrote

You can, and some devices do, use Newton's algorithm. For square roots, it consists of taking an initial guess z ; it can be pretty bad, say you guess sqrt(a) to just be a.

Then you calculate (z + a/z) / 2 *. This is a much better estimate. Now set that result to be z and plug it in again; and again; and again. It becomes a very good approximation to the square root pretty fast.

Example: calculating sqrt(5), starting with the estimate z=2 to get nice simple fractions instead of ugly decimals (a calculator obviously would not care, but I do).

  • Step 0: z=2 ; z² = 4.
  • Step 1: z = (2 + 5/2)/2 = 9/4 ; z² = 5.0625
  • Step 2: z = (9/4 + 5·4/9)/2 = 161/72 ; z² ~ 5.00019
  • Step 3: z = (161/72 + 5·72/161)/2 = 51841/23184 ; z² ~ 5.00000000186

It took us only 3 iterations to get 8 digits down. It can actually be shown that the number of correct digits roughly doubles each step. So with one more, we easily get the 10 or 12 digits most calculators use.

*: Note how (z + a/z) / 2 is in the middle between our previous guess z and a/z, which is another plausible guess (indeed, if z²=a, then a/z is just z; and if z is too small, then a/z is too large, and vice versa). The true reason behind the algorithm is based in calculus, one approximates using tangents at a curve to find places that should be closer to solutions.

18

eloel- t1_j23wkxy wrote

It's closer to a formula than it is to trial and error. There are mathematical methods to estimating square roots, and calculators do not need infinite precision. They iterate over a formula that gets more precise the more you apply it, looking for the desired precision, and then they return the result.

11

Target880 t1_j244m57 wrote

There is multiple way to calculate a square root https://en.wikipedia.org/wiki/Methods_of_computing_square_roots The simples to understand is iterative methods. The general idea is make a guess and then improve it.

without using any specific algorithm let's try to calculate the square root of 26. Let's find a to large and to small number and test the average of them. If the average is to small replace the smaller number, if it is to larger replace the larger

Let's first guess at 5 5 ^(2) =25, that is too low lets call it S

The second guess at 6 6^(2) =36 is to large let's call it L .

Let's try the midpoint (S+ L)/2 =(5+6)/2 = 5.5 and 5.5^2 =30.25 which is to large so replace S with it.

The next average is (5+5.5)/2=5.25 Let's repeat it in the table below. I rounded all numbers to 6 or less decimals

S        L        (S+L)/2        ((S+L)/2)^2
5        6           5.5            30.25        To large
5        5.5         5.25           27.5625      To large
5        5.25        5.125          26.265625    To large
5        5.125       5.0625         25.628906    To small
5.0625   5.125       5.09375        25.946289    To small
5.09375  5.125       5.109375       26.105716    To large
5.09375  5.109375    5.101563       26.02594     To large
5.09375  5.101563    5.097659       25.986102    To small
5.097659 5.101563    5.099611       26.006032    To large
5.097659 5.099611    5.098635       25.996079    To small
5.098635 5.099611    5.099123       26.001055    To large
5.098635 5.099123

Let's stop at this point. We can see that the square root is in between 5.098635 and 5.099123 so we know it to be two decimals as 5.09. We could continue this forever and get closer and closer. Many roots like this will have an infinite number of decimals so wee needs to stop.

The algorithm above is not an especially efficient one because it converges quite slowly but it is a good example of an iterative way do numerical find a solution to an equation.

The guesses I made were so you quickly get some decimals correctly. But you can pick a conservative guess like 1 and the number /2 that will work for any number we look for a root for that is larger than 1

Newton-Rapson https://en.wikipedia.org/wiki/Newton%27s_method method is faster, but exactly why you choose that formulais a lot harder to explain.

Let's call x the guess and a number we look for the root of in this case a=26. The next guess can be calculated 1/2( x + a/x) let's start with 5 and calculate the next number as 1/2 (5+26/5) = 5.1 . The next step is 1/2 (5.1+26/5.1)

I added a space among the decimal at the point it is longer accurate

5
5.1
5.099019 6078431372549019607843137254901960784313725490196078431372
5.09901951359278 57010906650681807043140270913210506275188406452756
5.099019513592784830028224109022 8563911005788636011794938296893309
5.099019513592784830028224109022781989563770946099596407584970804 9


The square root is
5.0990195135927848300282241090227819895637709460995964075849708044...

The number of accurate decimals doubles in each step. That is a very efficient way to find a square root of a number

8

cthulhu944 t1_j242d0n wrote

There is a mathematical concept called a taylor series that can be used to compute things like irrational roots. It is way beyond the scope of eli5. The basic concept is that you can repeatedly call this function and each iteration gets you closer to the real value.

2

FearlessFaa t1_j2455m7 wrote

Although partial sums of Taylor series use basic arithmetic operations so one could post an answer that calculates partial sums of Taylor series. We could then compare that with the average method described by other answers.

2

cthulhu944 t1_j24ssd7 wrote

You can use the average method to compute a square root, but the question was how a calculator computes it. That is definitely done by using a taylor series.

1

FearlessFaa t1_j24vms7 wrote

I think to the question it is unrelevant how physical calculators do it since physical calculators are not used much anymore. We are interested how to calculate square root i.e. different algorithms (=methods) used to calculate square root. It’s unrelevant what specific method calculators use.

1

cthulhu944 t1_j2a8kps wrote

Maybe I should clarify my point then. Taylor series is the way pretty much every system (calculator, computer, cell phone, etc) computes a square root. You can look into the c stadard library and find the the sqrt funtion is implemented with a taylor series. This is because it is computationally efficient and provides a predictable level of accuracy. The estimation algorithm that every other person on this thread has mentioned is really only used by people wanting to do a manual computation, or as an exercise in an introductory computer science class.

1

MrSuperHappyPants t1_j246qix wrote

Other people have posted great answers. The following doesn't really contribute much that's new to the discussion, but since I wrote it I'll paste it here - consider it a way of paraphrasing previous responses.


The bisection method is simple enough to try and eli5:

Consider the square root (sqrt) of 2. Find two test values that will "bracket" the root. 1^2 = 1 (1 is too low), 2^2 = 4 (2 is too high).

So, cut the size of this interval in half (hence the "bisection" thing). If 1.5 is still too big (it is), we throw out 2 and the new interval of interest is between 1 and 1.5 (if it were too small we'd throw out 1 and the new interval would be between 1.5 and 2). 1 is too small, 1.5 is too big. Okay.

So now we're working with the interval between 1 and 1.5, and have tested both endpoints. So we try the midpoint of that interval which is 1.25. That's too small; there's now no need to bother checking anything between 1 and 1.25, so toss out 1. Now we're working with numbers between 1.25 and 1.5.

Basically we keep checking intervals that are each half the length of the previous one, where we know one endpoint is too big and the other one is too small.

  1. Evaluate at the midpoint of an interval.
  2. Toss out the endpoint that matches the "too big" or "too small" nature of the result of step 1.
  3. Create a new interval accordingly and repeat.

There are faster methods than this, but they are a bit harder to eli5.

1

adam12349 t1_j249w39 wrote

Algorithms, I saw other methods in the commets, but I like this one:

The square root of a number like 9 is 3, 9 is called the radicand. Now if you have x^½ = y then x/y = y. So the radicand decided by the results is the result. 9/3 = 3.

So let's try the following for say 16.

Guess: for example 6.

16/6 = 2.6667

Lets take the average of your guess and this number:

(2.6667+6)/2 = 4.3334

And try this number:

16/4.3334 = 3.692

Take the average:

(3.692+4.3334)/2 = 4.01

16/4.01 = 3.99

(4.01+3.99)/2 = 4

16/4 = 4

So 4²=16, the square root of 16 is 4. And you find a number that is pretty close really quickly.

1