Advertisement

Square Roots:

Started by May 25, 2001 08:30 AM
25 comments, last by Prozak 23 years, 8 months ago
Hi all, i''ve discovered a method for calculationg square roots using only 2 variables (yes, 2) that increases in accuracy as the number increases. I''ll post here the solution to this problem, but would like to hear your comments on square roots, and their calculations. Thank you all, Hugo Ferreira lotsjunk@hotmail.com http://unitek3000.tripod.com
My comments would have to be:

If you''ve got something you think is useful to post, post it. If you haven''t, don''t bother with ''teaser'' posts....


Game production:
Good, quick, cheap: Choose two.
Game production:Good, quick, cheap: Choose two.
Advertisement
show the code already

Arkon
[QSoft Systems]
ok ok ok

basicly, the square root of number X is
in between B^2 and (B-1)^2, where B is the
smallest number that squared is above X.

so, B is the smallest number that B^2 > X

lets say that X is 17
B has to be 5, cause 5 is the smallest number,
that squared is bigger than 17.
So, the square number of 17 has to be between
5 and B-1, 4.
5^2=25
4^2=16

we clearly see that 17 is much close to 16 than
to 25.
This tells us that whatever the SR of 17 is,
it has to ve much close of 4, than of 5.

So, i''m betting that the decimal part will be
very small.

The hole equation is something like this:

SQRT ( X ) = (B-1) + ( 1- (( (B^2)-X ) / (B^2-(B-1)^2)) )

and it will work, and as X get bigger, the more
accurate this will be.

I''ll write it here in Pascal, so everybody can understand:

Function SQRT( N : Longint ) : Real;
var B:longint;
begin
B:=0;
repeat
B:=B+1;
until B*B>n; here we find our B

sqrt=(B-1)+(1-(((B*B)-X) div (B*B-((B-1)*(B-1))))
end;


THATS IT, SQUARE ROOT WITH ONLY TWO VARIABLES !!!

the cool thing about this is that you can change the function,
so you can calculate any kind of root:
Square Root = 2v4 = 2
2v16 = 4
2v25 = 5
3v8 = 2
3v27 = 3



So, our function was:

2vX = (B-1) + ( 1- (( (B^2)-X ) / (B^2-(B-1)^2)) )

But we can write it as:

KvX = (B-1) + ( 1- (( (B^K)-X ) / (B^K-(B-1)^2)) )

thats it
anymore questions email me at:

lotsjunk@hotmail.com



















end;








Thats all good, but my question is: why did you spend time doing that in pascal? theres got to be some good reason you wanted to do all this.
bad!
Dude, wake up !!!

i said i only wrote it in PASCAL cause
i wanted everybod to understand, ok?

geez....
they post without reading...

Advertisement
Everyone here uses C or C++

Ben
http://therabbithole.redback.inficad.com
Example

/* SQRT.C: This program calculates a square root. */

#include
#include
#include

void main( void )
{
double question = 45.35, answer;

answer = sqrt( question );
if( question < 0 )
printf( "Error: sqrt returns %.2f\n, answer" );
else
printf( "The square root of %.2f is %.2f\n", question, answer );
}


Output

The square root of 45.35 is 6.73


The anonymous poster above me doesn''t seem to be too bright.
The whole point of trying to find a new sqrt() function is
because the standard function takes upwards of 70 cycles to
complete, which is way too many for any game. I hope everybody
here knows how to use sqrt();.
Dear anonymous poster above, thank you.

It seems that a lot of coders have 2-neuron brains,
and i really didn''t want to flame in my own post,
but what i see is pitifull.
I WROTE PASCAL because I wanted everyone to understand it,
not because i dont know C (which i do, btw).
...and, i only posted the code, because i hoped it would help someone, because this kind of functions are all very sloooow.

(by the way, if you understood my last post, you would have realised that the root doesnt even have to be square, you can calculate any kind of root, and math libraries many times only give the square root)

I would like to thank everyone who takes the time to actually read/understand the posts.

I hope this didnt sound too harsh... and if it did, well
you guys asked for it in the first place.

Hugo Ferreira
lotsjunk@hotmail.com

This topic is closed to new replies.

Advertisement