Skip to main content
gamedev.net
🔒 Locked

sqrt

Started by devman5 Jan 18, 2002 at 8:50 AM 1 replies 500+ views
Original Post
devman5
devman5
hi. I was wondering if you guys could give me a hand with the following problem ( I know ye probably think it''s easy, but it''s being plauging me for quite a while): how do you define the square root of a number.I guess you can''t use an a/b format because of sqrt(2); what about newton''s method? what would you take as your first approximation? Or is there a better way of doing it???
devman5
rapso
rapso
on nvidias homepage in the developer section is an optimised approximation of sqrt.

rapso->greets();
DekuTree64
DekuTree64
Here''s one way to find a square root with variable accuracy:

  
float sqrt(float num, float accuracy)
{
float root = num, temp = num, diff = accuracy + 1.0f; //just make sure diff > accuracy


while(temp)
{
temp /= 4.0f;
root /= 2.0f;
}
while(diff > accuracy)
{
diff = abs(root - (root + num / root) / 2.0f);
}
return root;
}


Hopefully I didn''t mess it up converting it from the fixed-point version I use to this version with floats.
Also, I didn''t make this up, I read it in a post here a few months ago^^



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.