0

I understand that this is a pretty math-y question, but how do programs get square roots? From what I've read, this is something that is usually native to the cpu of a device, but I need to be able to do it, probably in c++ (although that's irrelevant).

The reason I need to know about this specifically is that I have an intranet server and I am getting started with crowdsourcing. For this, I am going to start with finding a lot of digits of a certain square root, like sqrt(17) or something.

This is the extent of what python provides is just math.sqrt()

I am going to make a client that can work with other identical clients, so I need complete control over the processes of the math. Heck, this question might not even have an answer, but thanks for your help anyway.

[edit] I got it working, this is the 'final' product of it: (many thanks to @djhaskin987)

def square_root(number):
    old_guess = 1
    guess = 2
    guesssquared = 0
    while round(guesssquared, 10) != round(number, 10):
        old_guess = guess
        guess = ((number / guess) + guess ) / 2
        print(guess)
        guesssquared = guess * guess
    return guess

solution = square_root(7) #finds square root of 7
print(solution)

Tyler Glen
  • 78
  • 5
  • 1
    From googling the title of your question: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots . I don't suspect that you will find a convenient way of parallelizing the task, if that's what you are describing. Known methods seem to iterate on an answer to produce a _slightly better answer_. – Drew Dormann Sep 17 '20 at 18:40
  • 1
    There are a whole bunch of algorithms you can use to find square roots if you don't like the one in the standard libraries. https://en.wikipedia.org/wiki/Methods_of_computing_square_roots – Pranav Hosangadi Sep 17 '20 at 18:40
  • "_...std::sqrt is required by the IEEE standard to be exact..."_ - source https://en.cppreference.com/w/cpp/numeric/math/sqrt – Richard Critten Sep 17 '20 at 18:40
  • The thing is, I am trying to make it able to be divided among multiple clients. – Tyler Glen Sep 17 '20 at 18:41
  • I think what I need is a structure of code that demonstrates how it is done by the computer. – Tyler Glen Sep 17 '20 at 18:42
  • Could you please clarify if you want an algorithm that computes a *lot* of digits for some cryptographic needs? – Bob__ Sep 17 '20 at 18:43
  • Please take the [tour], read [what's on-topic here](/help/on-topic) and [ask], and provide a [mre]. "Implement this feature for me" is off-topic for this site. You have to _make an honest attempt_, and then ask a specific question about your algorithm or technique. – Pranav Hosangadi Sep 17 '20 at 18:43
  • 2
    A lot of architectures provide square root at the hardware level. [Example](https://mudongliang.github.io/x86/html/file_module_x86_id_116.html). – François Andrieux Sep 17 '20 at 18:44
  • I'm not asking anyone to implement anything for me. But looked all over the place and I couldn't find how square roots were actually calculated. – Tyler Glen Sep 17 '20 at 18:45
  • I understand that square root functions are provided, but I need to have control over the design of the algorithm. – Tyler Glen Sep 17 '20 at 18:46
  • Are you asking how to optimize square root calculation in C++? – ks1322 Sep 17 '20 at 18:47
  • Not exactly, I just need to be able to control it – Tyler Glen Sep 17 '20 at 18:47
  • In your own code just don't use `std::sqrt` but use your own implementation. This is how you will control it in your code. – ks1322 Sep 17 '20 at 18:51
  • @TylerGlen not sure what sort of control you need such that the __exact__ guarantee is insufficient? – Richard Critten Sep 17 '20 at 19:39
  • 1
    Does this answer your question? [Writing your own square root function](https://stackoverflow.com/questions/1623375/writing-your-own-square-root-function) – JaMiT Sep 17 '20 at 19:49
  • Oh, right. Proposing a dupe on a question with two close votes closes it. Sorry. For your reference, some more answers: [How is the square root function implemented?](https://stackoverflow.com/questions/3581528/how-is-the-square-root-function-implemented) – JaMiT Sep 17 '20 at 19:50
  • Yes that's great! @djhaskin987's answer worked for me, with a little debugging. – Tyler Glen Sep 17 '20 at 19:51
  • The question was closed, but I added a solution in the edit (sorry it's a little messy). – Tyler Glen Sep 17 '20 at 20:02

2 Answers2

1

Computers use a method that people have actually been using since babylonian times:

def square_root(number):
    old_guess = 1
    guess = 2
    while old_guess != guess:
        old_guess = guess
        guess = ((number / guess) + guess ) / 2
    return guess
djhaskin987
  • 8,513
  • 1
  • 45
  • 81
  • Thanks! this is exactly what I was looking for. – Tyler Glen Sep 17 '20 at 18:47
  • Actually, this didn't work. I think it needs something to keep the 'guess' variable changing, I thing it's doing the same thing over and over. – Tyler Glen Sep 17 '20 at 19:06
  • it's because `check != number` is not strictly accurate. if guess is not changing, it's because it's arrived at the answer but float rounding is defeating us @TylerGlen – djhaskin987 Sep 17 '20 at 19:12
1

x86 has many sqrt in registry, starting with FSQRT for float.

In general, if your function is too complicated or has no implementation, and is C^\infty ("infinitely" differentiable), you can expand it into a polynom via Taylor expansion. This is extremely common in HPC.

Soleil
  • 4,891
  • 3
  • 27
  • 47