An algorithm to calculate square root??

For everything that's not in any way related to PureBasic. General chat etc...
merihevonen
Enthusiast
Enthusiast
Posts: 326
Joined: Mon Jan 01, 2007 7:20 pm

An algorithm to calculate square root??

Post 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 :twisted: )

Thanks!
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

but what does the sqrt function do in the background of the executable
It does !fsqrt, which is an fpu opcode.
User avatar
Comtois
Addict
Addict
Posts: 1433
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post 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)
Last edited by Comtois on Fri Feb 02, 2007 9:28 pm, edited 1 time in total.
Please correct my english
http://purebasic.developpez.com/
merihevonen
Enthusiast
Enthusiast
Posts: 326
Joined: Mon Jan 01, 2007 7:20 pm

Post 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! :lol:
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?
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post 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 :lol: )
User avatar
Comtois
Addict
Addict
Posts: 1433
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post 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)
Last edited by Comtois on Sat Feb 03, 2007 12:19 am, edited 1 time in total.
Please correct my english
http://purebasic.developpez.com/
User avatar
Comtois
Addict
Addict
Posts: 1433
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

Bonne_den_kule wrote:Can't it just be like this?
Yes but it's too easy , not funny :)
Please correct my english
http://purebasic.developpez.com/
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
merihevonen
Enthusiast
Enthusiast
Posts: 326
Joined: Mon Jan 01, 2007 7:20 pm

Post by merihevonen »

Thanks everybody in MEGA advance!
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Comtois wrote:Yes but it's too easy , not funny :)
+1 :wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Bonne_den_kule wrote: (Don't flame me thefool :lol: )

*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!
Post Reply