Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Square Roots?

Author
Message
Lonnie
21
Years of Service
User Offline
Joined: 28th May 2003
Location:
Posted: 24th Oct 2003 00:13
Hey,

Can someone please show me a piece of code that finds a square root of a number using Float Numbers(or what ever they are). Im asking this cause I want to program a caculator, that can find square roots, areas of Circles, Pyramids, Cilinders, spheres, and other such things.

Here is a link to a site that has a way to find a Square Root, But I dont know how to apply this to DarkBASIC Pro.
http://www.jimloy.com/algebra/sqrt0.htm

Thanks!

Do Print "Hello World"
Wait 2003 loop
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 24th Oct 2003 00:23 Edited at: 24th Oct 2003 00:29
a#=sqrt(9.0)

that any help?

DB likes to be told what the numbers are even when they are constants (hence the 9.0 to tell it it IS a float even though it`s actualy 9), the function is

sqrt(x)

and you can assine the result directly to a variable (but make sure it`s a float unless you want it truncated) or you could just use the result direct eg:-

a#=9.0
b#=sqrt(a#)
print b#

or

a#=9.0
print sqrt(a#)

give the same result, cheers.


Mentor.
David T
Retired Moderator
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 24th Oct 2003 00:28
lol so simple!

Fishie says:
"Those who fail to prepare prepare to fail"
"Rules are for the guidance of the wise and the blind obdeniency of the fools"
Lonnie
21
Years of Service
User Offline
Joined: 28th May 2003
Location:
Posted: 24th Oct 2003 00:39
Oh, lol!
I did not know DarkBASIC Pro had the Square Roots of things(should have known that)
Thanks!

And if any of you want some thing realy hard to do... Try finding the square root of a Number using

"How about the Binomial Theorem, which says:

(x+y)^n = x^n + nx^(n-1)y + n(n-1)y^2/2! + n(n-1)(n-2)y^3/3! ...
If we make x=1 and n=1/2 (square root), we get:

(1+y)^(1/2) = 1 + y/2 - y^2/4(2!) + 3y^3/8(3!) - 3(5)y^4/16(4!) + 3(5)(7)y^5/32(5!) ...
If we want to find the square root of some number z, then z=1+y, which means that y=z-1. But we find that y must be in the range -1<y<1, so 0<z<2, this series only works when z is between 0 and 2, not inclusive. We could use another series for z>=2. Let's find the series for (1+y)^(-1/2):

(1/(1+y))^(1/2) = 1 - y/2 + 3y^2/4(2!) + 3(5)y^3/8(3!) - 3(5)(7)y^4/16(4!) ...
Here, you can use z=(1/(1+y)), which makes y=1/z-1. And again -1<y<1, which makes z>1/2. So, with this series, we can find the square root of anything greater than 1/2. Actually, it's slow for z>10.


--------------------------------------------------------------------------------

Note: y^2 means y squared. y^(1/2) is the square root of y. I could have tidied up the equations with superscipts. But the equations would be messy anyway, in HTML. So, please forgive me for the notation."

Some long math formula... For what purpose, I know not, just for the chalenge try to make it into a code in DarkBASIC. That would be pretty cool.

Thanks for all the help! Now I can try to make a caculater!

But It would be nice if I could see a code using a more mathamaticle way of doing it then using one command.

Thanks!

Do Print "Hello World"
Wait 2003 loop
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 24th Oct 2003 01:26 Edited at: 24th Oct 2003 01:31
Here's one way of finding square root of any number not using db's sqrt function.



If your mansion house needs haunting, just call Rentaghost!
Lonnie
21
Years of Service
User Offline
Joined: 28th May 2003
Location:
Posted: 24th Oct 2003 01:36
Thanks Spooky!

Nice Job! Im impressed!

Thanks

Later!

Do Print "Hello World"
Wait 2003 loop
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 24th Oct 2003 01:39
And here's the version for cube root:



If your mansion house needs haunting, just call Rentaghost!
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 24th Oct 2003 07:31
you could also use a binary chop routine to find a square root. When I first read your topic, I guessed that you were probably given the assignment from school. I think every programming class makes your write a sqrt function. If you ever have to do that, don't use x^0.2, cause teachers don't like a smartass that find easy work arounds of the hw.
Lonnie
21
Years of Service
User Offline
Joined: 28th May 2003
Location:
Posted: 30th Oct 2003 03:29
How would to the Power of 0.2 Help? I know Im only a Newb . But I just tried showing my dad how to find the Square Root with that, But I dont understand how it would work... wouldnt you need to do something after that?

Thanks!

Do Print "Hello World"
Wait 2003 loop
heartbone
22
Years of Service
User Offline
Joined: 9th Nov 2002
Location:
Posted: 30th Oct 2003 03:48 Edited at: 30th Oct 2003 03:51
You sure would.

X^(0.2+0.3)= Sqrt(X)

The more you see, the more you know.
The more you know, the more you see.

Login to post a reply

Server time is: 2024-11-25 14:37:23
Your offset time is: 2024-11-25 14:37:23