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.

DarkBASIC Discussion / help on square roots

Author
Message
sirsiddy
19
Years of Service
User Offline
Joined: 15th Jan 2007
Location:
Posted: 8th May 2007 02:23
erm dunno whats happening right now, try to make a square root calculator. I feel really stupid, feel like im doing everything wrong.... lol havnt programmed in a while

Am I doing it completely wrong?


input "enter a number to find the square root",z
squareroot(z)
cls
print t
end

function squareroot(z)
do
b = b + 1
b*b=t
if t = z then exit
loop
endfunction

forgot the how to do the text box thingy
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 8th May 2007 03:22
that won't even compile, and even if it did the function would only ever exit in the case of a perfect square. You cant assign 't' to b*b, you have to flip the sides around.


heartbone
23
Years of Service
User Offline
Joined: 9th Nov 2002
Location:
Posted: 8th May 2007 03:47


I'm unique, just like everybody else.
sirsiddy
19
Years of Service
User Offline
Joined: 15th Jan 2007
Location:
Posted: 8th May 2007 05:27
got it to work

here it is

input "enter a number to find the square root",z#
squareroot(z#)
cls
print b#
end

function squareroot(z#)
do
b# = b# + 1
t#=b#*b#
if t# = z#
print b#
endif
loop
endfunction
LBFN
19
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 8th May 2007 15:44
@ sirsiddy

The code you posted only works if the square root is a perfect square (i.e. 9=3 81=9, etc.) If you enter 112, for example, it just locks up.

Heartbone's code works fine and is shorter and cleaner. You should just use that.

LB
Zotoaster
21
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 8th May 2007 15:49
Or just do: sqrt = num ^ 0.5

TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 8th May 2007 19:36
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 8th May 2007 20:10
Lots of teachers have students write their own square root function. It's a simple, yet good thing to learn for any programmer.


Login to post a reply

Server time is: 2026-07-05 18:38:30
Your offset time is: 2026-07-05 18:38:30