Page 1 of 1

Square rooting

Posted: Mon Jun 26, 2006 10:16 pm
by AndrewAPrice
I've solved the squarerooting function of my library.. I thought I should share it if anyone else is interested.. It's written in Javascript just so I could test it.

Code: Select all

function squareroot(number)
{
	var answer = 0;
	if (answer > 10000*10000)
	{
		answer = 50000;
	}
	if (answer <= 10000*10000)
	{
		answer = 5000;
	}
	if (answer <= 1000*1000)
	{
		answer = 500;
	}
	if (answer <= 100*100)
	{
		answer = 50;
	}
	if (answer <= 10*10)
	{
		answer = 5;
	}

	while (((answer+1)*(answer+1) <number> number))
	{
		if(answer*answer <number> number)
		{
			answer = answer - 1;
		}
	}

	return answer;
}
Does anyone know how to return a decimal value as my function rounds down?

Re: Square rooting

Posted: Tue Jun 27, 2006 10:05 am
by rexlunae
MessiahAndrw wrote:I've solved the squarerooting function of my library.. I thought I should share it if anyone else is interested.. It's written in Javascript just so I could test it.

Code: Select all

function squareroot(number)
{
	var answer = 0;
	if (answer > 10000*10000)
	{
		answer = 50000;
	}
	if (answer <= 10000*10000)
	{
		answer = 5000;
	}
	if (answer <= 1000*1000)
	{
		answer = 500;
	}
	if (answer <= 100*100)
	{
		answer = 50;
	}
	if (answer <= 10*10)
	{
		answer = 5;
	}

	while (((answer+1)*(answer+1) <number> number))
	{
		if(answer*answer <number> number)
		{
			answer = answer - 1;
		}
	}

	return answer;
}
Does anyone know how to return a decimal value as my function rounds down?
I hope I'm not the only one to whom this looks like nonsense. I don't really know Javascript well, but I'm pretty sure this just returns 5 every time (I tried it and that is what it did for me).

Code: Select all

	var answer = 0;
So, first you set a variable

Code: Select all

	if (answer > 10000*10000)
	{
		answer = 50000;
	}
	if (answer <= 10000*10000)
	...
Then you do a bunch of comparisons. The first one always fails, then the rest always succeed. So, by the time the loop runs, the variable 'answer' is always == 5.

Code: Select all

while (((answer+1)*(answer+1) <number> number))
I don't know what <number> does, as I don't really know javascript, but with the several values I tried, this loop had no effect on the outcome, and always returned 5, so I don't think your loop is actually doing anything.

Correct me if I'm wrong, or if I missed something. Anyway, just one more comment: Please tell me you aren't trying to write an OS in Javascript.

Re: Square rooting

Posted: Tue Jun 27, 2006 10:14 pm
by AndrewAPrice
rexlunae wrote: Anyway, just one more comment: Please tell me you aren't trying to write an OS in Javascript.
Nope, I'm using FASM for my OS. I used Javascript because I was able to quickly implement the function that I had in my head.

Re: Square rooting

Posted: Wed Jun 28, 2006 9:17 am
by rexlunae
MessiahAndrw wrote:
rexlunae wrote: Anyway, just one more comment: Please tell me you aren't trying to write an OS in Javascript.
Nope, I'm using FASM for my OS. I used Javascript because I was able to quickly implement the function that I had in my head.
So, how is this function supposed to work?

Posted: Wed Jun 28, 2006 1:56 pm
by gaf
Hello
You could use the quite easy to implement "Babylonian method" to calculate your square-roots:

Code: Select all

double my_sqrt(double value)
{
    double error = 0.000001f;
    double side  = 1;
    
    while(abs(pow(side, 2) - value) > error)
    	side = (side + value/side)/2;
    	
    return rect;
}
The basic idea is to think of the original value as the size of a rectangle that is initially 'value' wide and 'side' high. The alorithm now tries to turn the rectangle into a regular square with equal sized sides. To do so the mean value of the two old sides is calculated and the lengths get updated:

Code: Select all

x(n+1) = (x(n) + y(n))/2    =>    x(n+1) = (x(n) + a/x(n))/2
regards,
gaf

Re: Square rooting

Posted: Wed Jun 28, 2006 9:32 pm
by |333173|3|_||3
rexlunae wrote:
MessiahAndrw wrote:
rexlunae wrote: Anyway, just one more comment: Please tell me you aren't trying to write an OS in Javascript.
Nope, I'm using FASM for my OS. I used Javascript because I was able to quickly implement the function that I had in my head.
So, how is this function supposed to work?
Quite simple really. He didn't post the code right.
MessiahAndrw wrote:

Code: Select all

function squareroot(number) 
{ 
   var answer = 0; 
   if (answer > 10000*10000) 
   { 
      answer = 50000; 
   } 
   if (answer <= 10000*10000) 
   { 
      answer = 5000; 
   } 
   if (answer <= 1000*1000) 
   { 
      answer = 500; 
   } 
   if (answer <= 100*100) 
   { 
      answer = 50; 
   } 
   if (answer <= 10*10) 
   { 
      answer = 5; 
   } 

   while (((answer+1)*(answer+1) <number> number)) 
   { 
      if(answer*answer <number> number) 
      { 
         answer = answer - 1; 
      } 
   } 

   return answer; 
}
I know that this works because I have seen him run the Javascript version. The function has to be passed "number" by a call in the HTML body of the page it is in:

Code: Select all

<SCRIPT>
     squareroot(*)
</Script>
Obvoiusly the * would be replaced by the number to be square-rooted. if the number is greater than 100000000 then it begins with a value of the square root which is 50000, if less then itgoes through a seris of tests to determine what range the nuber is in. THis is not stricly necessary, since if answer beings at zero it will eventually reach the required value, but is merely part of the optimising MessiahAndrw used. Answer is only set to 5 if the number is less than 100.
The next part of the code is where think there is a mistake, since I think he mis-copied the script fromt he HTML document. I think that if:

Code: Select all

<number>
is replaced by != then it might work.
so far I have come up with

Code: Select all

<HTML>
<HEAD>
<SCRIPT>
function squareroot(number)
{
   var answer = 0; 
   if (answer > 10000*10000) 
   { 
      answer = 50000; 
   } 
   if (answer <= 10000*10000) 
   { 
      answer = 5000; 
   } 
   if (answer <= 1000*1000) 
   { 
      answer = 500; 
   } 
   if (answer <= 100*100) 
   { 
      answer = 50; 
   } 
   if (answer <= 10*10) 
   { 
      answer = 5; 
   } 

   while (((answer+1)*(answer+1) <number> number)) 
   { 
      if(answer*answer <number> number) 
      { 
         answer = answer - 1; 
      } 
   } 

   alert(answer); 
}
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT>
	var a=prompt("enter the number to square-root","");
	squareroot(a);
</SCRIPT>
dskih
</BODY>
</HTML>
to try to get it to work, but It dosent seem to yet.
I will ask messiahAndrw to rep post the entire HTML page, which I know works, and then we can see how he did it. I know this method will always round the answer down, not up.

Posted: Wed Jun 28, 2006 11:13 pm
by |333173|3|_||3
Update:
I spoke to MessiahAndrw, he says that the database software screwed up, and that the <number> which confused rexlunae (and me) was actually originally a long, complicated mathemaitial and logical expression.

Posted: Thu Jun 29, 2006 4:05 am
by gaf
Answer is only set to 5 if the number is less than 100.
Ehm, you did notice that 'number' isn't metioned in any of the if-statements ? Therefore you could replace the whole "optimization" with something as simple as "var answer = 5". As rexlunae said, it always boils down to that value..
I think that if <number> is replaced by != then it might work.
That would be some improvement, but it still wouldn't work. One problem is that your while-loop checks the original number agains (answer+1)^2 and not just answer^2, the other problem is that answer is initialized with 5 rather than some much higher value (your code only works for sqrt(x) smaller 5). Even if you fixed these two bugs the problem itself persists: You can only calculate integer (rounded up/down) square-roots.

Code: Select all

function squareroot(number) 
{ 
   var answer = 0;

   while (answer*answer < number)
   {
       answer = answer+1;
   } 
   alert(answer); 
}
If you want to have a real-number result (eg: sqrt(2) = 1.4142, rather than 1 < sqrt(2) < 2) you have to use floating-point arithmetics. This however can hardly be realized with your approach as the granularity one would have to use to increase answer in the loop would have to be very small (something like: answer = answer - 0.00000000000001). You can guess that it would take ages until you finally reach the actual number..

So what's wrong with my algorithm ? It's been used for a couple of millenia and I dare say that it will also do for your purposes.
I spoke to MessiahAndrw, he says that the database software screwed up, and that the <number> which confused rexlunae (and me) was actually originally a long, complicated mathemaitial and logical expression.
Too bad that he lost it, I would have loved to have a look at it.. (scnr)

regards,
gaf

Posted: Sat Jul 01, 2006 7:38 am
by Legend
gaf wrote:Hello
You could use the quite easy to implement "Babylonian method" to calculate your square-roots:
While it is simple, I want to add that is probably still one of the best algorithmus to find the square root. ;)