-1

How to approximate sqrt(float32bit x) if I know that x<=1?

There have to be some tricks to exploit the range x<=1.

The return result doesn't have to be precise, the maximum error may be <0.001.
(0.001 is just a magic number, you can change it.)

I don't mind language, but I prefer C++, in CPU (not GPU).
I think explicit formula is better than table look-up.

It is useful for particles in my 3D game (VS 2015 + Ogre3D + Bullet), and I can't find any clue about it.
I doubt the reason of the downvote storm is that it looks like an assignment / interview?
... or the solution is already well-known?

javaLover
  • 6,039
  • 2
  • 14
  • 57

1 Answers1

2

Square roots are ordinarily computed through FSQRT which getting increasingly faster. It is a single instruction that you practically can't beat. The fast inverse square root for example won't help unless you can use its result directly. If you have to invert it again, that FDIV alone will take roughly as much time as FSQRT would have.

Community
  • 1
  • 1
back2dos
  • 15,346
  • 31
  • 48
  • Thank! That is a valuable information! Now I will stop seeking this thing. – javaLover Jan 11 '17 at 04:10
  • You don't need to divide to use the inverse square root. `1/(1/√x)` == `x*(1/√x)`. – Mark Ransom Jan 11 '17 at 19:25
  • @javaLover before you stop seeking you might want to look at [Why is SSE scalar sqrt(x) slower than rsqrt(x) * x?](http://stackoverflow.com/questions/1528727/why-is-sse-scalar-sqrtx-slower-than-rsqrtx-x). – Mark Ransom Jan 11 '17 at 20:02
  • @MarkRansom: Except when x=0 (and infinity, but that's not of interest here), so that has to be handled as a special case with the multiplicative approach since it is included in the interval of interest to the asker. – njuffa Jan 31 '17 at 17:53