For everything that's not in any way related to PureBasic. General chat etc...
merihevonen
Enthusiast
Posts: 326 Joined: Mon Jan 01, 2007 7:20 pm
Post
by merihevonen » Fri Feb 02, 2007 6:44 pm
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!
va!n
Addict
Posts: 1104 Joined: Wed Apr 20, 2005 12:48 pm
Post
by va!n » Fri Feb 02, 2007 6:46 pm
va!n aka Thorsten
Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Trond
Always Here
Posts: 7446 Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway
Post
by Trond » Fri Feb 02, 2007 6:46 pm
but what does the sqrt function do in the background of the executable
It does !fsqrt, which is an fpu opcode.
Comtois
Addict
Posts: 1433 Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France
Post
by Comtois » Fri Feb 02, 2007 9:25 pm
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.
merihevonen
Enthusiast
Posts: 326 Joined: Mon Jan 01, 2007 7:20 pm
Post
by merihevonen » Fri Feb 02, 2007 9:27 pm
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?
Bonne_den_kule
Addict
Posts: 841 Joined: Mon Jun 07, 2004 7:10 pm
Post
by Bonne_den_kule » Sat Feb 03, 2007 12:04 am
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
)
Comtois
Addict
Posts: 1433 Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France
Post
by Comtois » Sat Feb 03, 2007 12:11 am
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.
Comtois
Addict
Posts: 1433 Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France
Post
by Comtois » Sat Feb 03, 2007 12:16 am
Bonne_den_kule wrote: Can't it just be like this?
Yes but it's too easy , not funny
PB
PureBasic Expert
Posts: 7581 Joined: Fri Apr 25, 2003 5:24 pm
Post
by PB » Sat Feb 03, 2007 12:17 am
> 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
Posts: 326 Joined: Mon Jan 01, 2007 7:20 pm
Post
by merihevonen » Sat Feb 03, 2007 12:55 am
Thanks everybody in MEGA advance!
Psychophanta
Always Here
Posts: 5153 Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:
Post
by Psychophanta » Sat Feb 03, 2007 12:11 pm
Comtois wrote: Yes but it's too easy , not funny
+1
thefool
Always Here
Posts: 5875 Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark
Post
by thefool » Sat Feb 03, 2007 12:53 pm
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!