32

How does the computer calculate Square roots ? I mean what is going on there! How does it process it!! Does it use some mathematical ways like Newton's method? What about Trigonometric Functions? And almost all those Mathematical Functions . In the case that every language has its own way, then please let's talk about c++.

AbraCadaver
  • 73,820
  • 7
  • 55
  • 81
Loers Antario
  • 1,341
  • 4
  • 15
  • 23
  • 4
    Your question is too broad. Take a look at http://en.wikipedia.org/wiki/Methods_of_computing_square_roots – Seçkin Savaşçı Sep 06 '12 at 16:42
  • possible duplicate of [How does C compute sin() and other math functions?](http://stackoverflow.com/questions/2284860/how-does-c-compute-sin-and-other-math-functions) – tenfour Sep 06 '12 at 16:43

4 Answers4

33

Most modern non-embedded CPUs (x86 and the larger ARM cores, for example) have hardware instructions to compute square roots directly. The hardware implementation backing these instructions varies, but typically is a variant on the schoolbook digit-by-digit algorithm (though not always in base two; base four or sixteen can also be used). These are typically among the slowest basic arithmetic operations on a CPU; timings like 16-64 cycles are not uncommon, and these instructions are often not pipelined.

On CPUs that lack direct hardware square root instructions (Itanium, PPC, others), the typical approach is to generate an initial estimate (either with an instruction that produces the estimate, or with a lookup table) and then refine that estimate using an iterative method (Newton or Goldschmidt usually). You might track down some of Peter Markstein or Roger Golliver's writings on the subject if you're interested.

More complex mathematical functions (like trig operations) are typically computed by reducing the argument into some fundamental domain and then approximating it with a polynomial or rational function. You can look at the sources of any of several math libraries that are available online for more detail (fdlibm is a good starting point).

The x86 instruction set provides a number of instructions that support mathematical functions like exp, log, and sin, but these are not commonly used anymore, because good software library implementations give better performance.

Stephen Canon
  • 97,302
  • 18
  • 172
  • 256
  • On the topic of software square root: Are you the author of this code? https://opensource.apple.com/source/Libm/Libm-2026/Source/ARM/sqrt.c.auto.html What is the license? Can it be used under GPLv3? Thanks. – airafr Nov 20 '18 at 19:26
  • 1
    @airafr the license on that file, like most of Apple’s open source, is APSL v2. – Stephen Canon Nov 22 '18 at 13:08
6

Another possibility that hasn't been mentioned, is the CORDIC method. CORDIC isn't widely used/known in software, but is pretty common in hardware, and does pretty well at getting decent performance without using a lot of gates.

Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
3

I think Newton's iterative convergence method is use in calculating Square Root

user1598202
  • 240
  • 1
  • 2
2

As others have pointed out, this is a very broad question - what works well for software might be a poor choice for a hardware implementation; and then there are issues of correct rounding for IEEE-754, hardware lookup tables, etc. There are a lot of open source C libs, with libm implementations as well. A good overview of classic and modern methods can be found here.

Brett Hale
  • 20,019
  • 2
  • 47
  • 81