Page 1 of 1
An algorithm to calculate square root??
Posted: Fri Feb 02, 2007 6:44 pm
by merihevonen
Hello!
Is there an algorithm to mathematically solve a square root expression??
For example, sqrt(25) equals 5, but what does the sqrt function do in the background of the executable?
Pseudo/Pure/ASM code is welcome.. I'm just making a Learn-O-Mathic program for 7+ kids to learn maths (Ofcourse, I'll sell it for 5000$ and that as a proprietary product only for Mac users

)
Thanks!
Posted: Fri Feb 02, 2007 6:46 pm
by va!n
Posted: Fri Feb 02, 2007 6:46 pm
by Trond
but what does the sqrt function do in the background of the executable
It does !fsqrt, which is an fpu opcode.
Posted: Fri Feb 02, 2007 9:25 pm
by Comtois
Code: Select all
Procedure.f Sqrt(n.f)
ProcedureReturn Pow(10,Log10(n)/2.0)
EndProcedure
Procedure.f Racine(n.f, r)
ProcedureReturn Pow(10,Log10(n)/r)
EndProcedure
a.f = 125.0
r = 3
Debug Sqrt(a)
Debug Sqr(a)
Debug Racine(a,r)
Debug Pow(a,1/r)
Posted: Fri Feb 02, 2007 9:27 pm
by merihevonen
Comtois wrote:Procedure.f Sqrt(n.f)
ProcedureReturn Pow(10,Log10(n)/2.0)
EndProcedure
a.f = 125.0
Debug Sqrt(a)
Debug Sqr(a)
I love you!
But I'm not gay.. I "love" you in the sense that I appreciate it a lot that you helped me.
EDIT: But how do I make a "pure" version of Log10?
Posted: Sat Feb 03, 2007 12:04 am
by Bonne_den_kule
Code: Select all
Procedure.f Sqrt(n.f)
ProcedureReturn Pow(n.f,0.5)
EndProcedure
Debug Sqrt(100)
Did I misunderstood something?
Can't it just be like this?
(Don't flame me thefool

)
Posted: Sat Feb 03, 2007 12:11 am
by Comtois
Code: Select all
Procedure.f Sqrt(n.f)
ProcedureReturn Pow(10,Log10(n)/2.0)
EndProcedure
Procedure.f Sqrt2(n.f)
precision.f = 0.001
s.f = 1.0
r.f = 0.0
Repeat
r=(s + n / s) / 2.0
s=r
test.f = r*r - n
Until Abs(test) < precision
ProcedureReturn r
EndProcedure
Procedure.f InvSqrt(x.f)
xhalf.f = 0.5*x;
i = PeekL(@x); // get bits for floating value
i = $5f3759df - (i >> 1); // gives initial guess y0
x = PeekF(@i); // convert bits back to float
x = x * (1.5 - xhalf * x * x); // Newton step, repeating increases accuracy
ProcedureReturn x
EndProcedure
a.f = 671.0
Debug Sqrt(a)
Debug Sqrt2(a)
Debug a*InvSqrt(a) ; http://www.math.purdue.edu/~clomont/Math/Papers/2003/InvSqrt.pdf
Debug Sqr(a)
Debug Pow(a,1.0/2.0)
Posted: Sat Feb 03, 2007 12:16 am
by Comtois
Bonne_den_kule wrote:Can't it just be like this?
Yes but it's too easy , not funny

Posted: Sat Feb 03, 2007 12:17 am
by PB
> Can't it just be like this?
Yep. Any number to the power of 0.5 is the square root. To modify yours:
Code: Select all
Debug Pow(9,0.5) ; Returns square root of 9
Posted: Sat Feb 03, 2007 12:55 am
by merihevonen
Thanks everybody in MEGA advance!
Posted: Sat Feb 03, 2007 12:11 pm
by Psychophanta
Comtois wrote:Yes but it's too easy , not funny

+1

Posted: Sat Feb 03, 2007 12:53 pm
by thefool
Bonne_den_kule wrote:
(Don't flame me thefool

)
*POW*
Well i wont flame you as your method is obviously the simplest (Hence its the smartest too!)

I've used it quite a lot dealing with dirty equations hehe
keep things simple!