Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ageny6

A mad fast SquareRoot method

This topic is 5452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know that for a fact the Squareroot method is really slow! But, unfortunatly, I got to this point where I need it I there a substitute method that is much faster than the SquareRoot method given in the Math.h library (or the sqr function if you are using VB)? My signature used to suck. But it''s much better now.

Share this post


Link to post
Share on other sites
Advertisement
if you have a new compiler, than float sqrt(float) in your math.h is fast enough. Period. there are a lot of impl using integers in the forums, but integer arith ceased to be faster in 1995. FPU is a lot faster now.

Share this post


Link to post
Share on other sites
and if you dont have a new compiler download support libraries from amd and intel which use the faster sqrt functions that their processors provide. [or u could use asm emit statements...]

im not to sure if interger/bit hacks are slower these days, but i wouldn''t be suprised.

Share this post


Link to post
Share on other sites
quote:
Original post by Lorenzo
if you have a new compiler, than float sqrt(float) in your math.h is fast enough.

How fast enough?

---------------------------------------------
And is there an algorithm at the higher level language that performs the squareroot quickly?


My signature used to suck. But it''s much better now.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you are using sqrt for distances try an approximation. http://www.flipcode.com/articles/article_fastdistance.shtml

sometimes you can try getting rid of the square root.

( a * a > (b * b))

vs

(sqrt(a * a) > b )

Share this post


Link to post
Share on other sites
This is called micro-optimization, and it's dumb. Instead, if sqrt proves to be too slow, do something about it. Until it does, don't worry about it. Most often, the "doing something" will be optimizing your algorithm at a higher level, which does not include finding a faster sqrt. As someone said, any recent ('99+) compiler will have a plenty fast sqrt.

"How fast enough?" Fast enough that you don't need to worry about it. Just use it.

Later,
ZE.

[edited by - zealouselixir on October 13, 2003 4:30:51 PM]

Share this post


Link to post
Share on other sites
I wrote a new sqrt based on MacLaurin expansions (along with sin and cos). Never needed them, finally. No noticeable gain in anything.

Debating using a lookup table, but the real thing is this--you have way more important things to speed up than your sqrt routine. Don''t even worry about it until you have a playable game that is cool enough to show off to everybody.

Share this post


Link to post
Share on other sites
Gives the inverse of the square root (floats only):


float InvSqrt (float x)
{
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i >> 1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}

That was on these forums a while ago, credit to Carmack but really discovered by someone else. I need sleep now.

Share this post


Link to post
Share on other sites
quote:
Original post by kordova
That was on these forums a while ago, credit to Carmack but really discovered by someone else. I need sleep now.


the Newton-Raphson method.. although, the theory behind it is a hundred years old if im not mistaken.

-eldee
;another space monkey;
[ Forced Evolution Studios ]


::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!