4

I'm looking right now at particular algorithm for calculating square root which returns the integer part of the square root and the remainder.

So for example: mysqrt(140) = 11*11 + 19 = integer 11, remainder 19

The question is can I calculate the square root as float for example the square root of 140 is ~ 11.8321....?

edit from comments

I'm looking at VHDL implementation of a fixed point square root which uses only binary operations like left/right shift, addition and substraction.

...algorithm would be enough.

EDIT 2 I am actually reading this algorithm here: http://pioneer.netserv.chula.ac.th/~achatcha/Publications/0012.pdf

It seems that a better precision could be made by shifting left the radicand by 2n. I am not quite sure why that is working? Could anyone please explain me

  • I've fixed what looked like a typo (`remainder 9` instead of `19`). – NPE Jan 17 '12 at 12:13
  • This basically amounts to writing your own [square root implementation](http://stackoverflow.com/questions/1623375/). – outis Jan 17 '12 at 12:25
  • Programming language or just algorithm? – ChrisBD Jan 17 '12 at 12:25
  • @ChrisBD, algorithm would be enough. – mark mcgehee Jan 17 '12 at 12:27
  • Clearly you can but what operations are you limiting yourself to? Presumably you don't want to use a full sqrt operator. There are algorithms that use estimation and remainders to work out square roots. http://www.homeschoolmath.net/teaching/square-root-algorithm.php is one such. – Chris Jan 17 '12 at 12:27
  • Thanks @Chris, i'll take a look. The algorithm which I'm looking right now (to find the int, and the remainder) uses only binary operations like left/right shift, addition and substraction – mark mcgehee Jan 17 '12 at 12:30
  • Thanks @ChrisBD for editing the question. – mark mcgehee Jan 17 '12 at 12:40

3 Answers3

4
(11+x)^2 = 140
11^2 + 2*11*x + x^2 = 140
2*11*x + x^2 = 19
x^2 + 2*11*x - 19 = 0

To solve that, you need to do another sqrt:

x = -11 + sqrt((2*11)^2 + 4 * 19) / 2

Or for the final answer:

11+x = sqrt((2*11)^2 + 4 * 19) / 2

This isn't faster than just doing

sqrt(140)

If you're looking for a quick approximation:

x^2 + 2*11*x - 19 = 0
x = (19 - x^2)/(2*11)

Guessing x = 0.5, gives

x = 19/(2*11) - 0.5*0.5/(2*11) = 0.852272727

You could apply this repeatedly to arrive at better approximations, but Newton's method is probably faster.

Ishtar
  • 11,121
  • 1
  • 21
  • 30
  • It's actually something complete different. I'm looking at VHDL implementation of a fixed point square root which uses only binary operations like left/right shift, addition and substraction. – mark mcgehee Jan 17 '12 at 12:32
  • To clarify, this is a proof that the answer to your question is no, you cannot calculate the floating point square root given the integer and remainder. (at least that's what I get from it...) – Justin Jan 17 '12 at 12:35
  • @Ishtar, sorry I thought there is obvious (simple) solution which I don't see at that time – mark mcgehee Jan 17 '12 at 12:36
1

In response to:

It seems that a better precision could be made by shifting left the radicand by 2n. I am not quite sure why that is working? Could anyone please explain me

The paper you have linked talks about the left shifting by 2n. The reason it works is becasue you are effectively shifting by a multiple of 4 which is easy to factor into the square root.

sqrt(K*2^2n) = sqrt(K)*sqrt(2^2n) = sqrt(K)*2^n

So you just shift back by n bits and you get the right answer. If you keep those shifted bits as decimal parts then you get your fractional answer.

Think of it in decimal terms of multiplying by 100 before the square root and dividing by 10 after.

So

sqrt(2) = sqrt(200)/10 = 14/10 = 1.4

Where sqrt(200) is only giving an integer.

Chris
  • 26,164
  • 4
  • 67
  • 85
0

I'm not sure I understand your question. Do you want to know how to use the sqrt function on a float, or how to write your own?

If it's the former, then your language will provide something (probably called sqrt()). If it's the latter, then you need to look up some sort of numerical resource. I would recommend GSL: http://www.gnu.org/software/gsl/

invisiblerhino
  • 780
  • 1
  • 7
  • 18
  • Nope, I know how to use it :) I'm not using the system sqrt which is implemented already, but another algorithm which return the integer part of the number and the remainder (see the example) – mark mcgehee Jan 17 '12 at 12:21
  • So given int part = 11 and remainder = 19, what's the float equivalent? – invisiblerhino Jan 17 '12 at 12:24