Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → square root
square root
2004-10-13, 5:09 PM #1
Due to a current lack of square root code around massassi (as far as I can tell). I decided to give you my temporary version until my math teacher can get me the actual formula.
Code:
# Jedi Knight Cog Script
#
# square_root.cog
#
# Finds the Square root of a number, rootme.
#
# SG-fan
#

symbols
	message	activated
	
	surface	button
	flex		a		local
	flex		b		local
	flex		answer		local
	flex		rootme			//number to be rooted
end

code
activated:
call sqrt;
	return;

sqrt:
for(a=0; rootme-a*a>=0; a=a+1)
{}
if(rootme-a*a>rootme-(a-1)*(a-1))
a=a-1;
b=rootme-a*a;
answer=(a+(b/(2*a)))-(((b/(2*a))*(b/(2*a)))/(2*(a+(b/(2*a)))));
printflex(answer);
return;
end



Feel free to use the code however you see fit, but please credit me somehow.

[edit] Just so you know, I put in the call message so that it would be easilly accessed by ANYTHING (you can call it from anywhere). [/edit]
[edit2] I forgot to mention, it works for roughly 4 decimals [/edit2]
Sam: "Sir we can't call it 'The Enterprise'"
Jack: "Why not!"
2004-10-13, 5:19 PM #2
wouldn't it just be easier if you put the number to a power of a half?
2004-10-13, 5:45 PM #3
I don't know of a way to put a number to a power in JK/MOTS. If you do, please enlighten me.

[edit] Wow, I can't believe I missed it. I did about 15 different test searching for an exponential function before looking up this code, with no avail. After reading your post it hit me. I forgot to try Pow(num, power). I feel really dumb now. [/edit]
Sam: "Sir we can't call it 'The Enterprise'"
Jack: "Why not!"

↑ Up to the top!