Square root alternative?

what class is this for? (i.e., do your own homework...)

Do you know how to calculate a square root without using a square root function on a calculator?
 
Yes, but Im just starting off and I don't know how to create the appropriate code within the PLC.
Im not in school anymore. I took Electronic Engineering Tech, but never covered PLC's. I am just curious and want to experiment with them.
 
Last edited:
I had just found a fast cube root routine and was looking for a similar square root

Code:
    double fsqrt (double y) {
    double x, z, tempf;
    unsigned long *tfptr = ((unsigned long *)&tempf) + 1;

	tempf = y;
	*tfptr = (0xbfcdd90a - *tfptr)>>1; /* estimate of 1/sqrt(y) */
	x =  tempf;
	z =  y*0.5;                        /* hoist out the “/2”    */
	x = (1.5*x) - (x*x)*(x*z);         /* iteration formula     */
	x = (1.5*x) – (x*x)*(x*z);
	x = (1.5*x) – (x*x)*(x*z);
	x = (1.5*x) – (x*x)*(x*z);
	x = (1.5*x) – (x*x)*(x*z);
	return x*y;
    }
[/quote]
No divides!!!!!  This is important because the divide can be 5 to 40 times slower than a multiply on a DSP or micro controller.
 
Now, why would you want to do that?

Does anyone know how to easily take the square root of a number without using a square root function in PLC language?


Well, you could always do it the way it used to done: take the log, divide by 2, and then take the antilog (exponentiation). :) [Yea, yea, probably faster to multiply by .5, but it seemed silly to worry about the cost of division when I was using expensive log and exponentiation functions. :) Besides, in many of these PLC and scripting environments, the high cost of division gets buried under a runtime cost that is primarily dependent on how many instructions are executed.]

Of course, if the target platform does not have a square root function, it probably does not have log and exp functions either, so you'll have to use the techniques provided by the other posts.
http://ilab.usc.edu/wiki/index.php/Fast_Square_Root also provides some fast approximations.

An obvious question is why do you want to take a square root? I see that in one of the "Similar Topics" threads, the Opening Poster wanted to take square roots because he wanted to draw circles. You don't want to do this. Here are some links to circle drawing.

Bresenham's Line and Circle Algorithms
http://www.gamedev.net/reference/articles/article767.asp

Some of "the wonderful coding tricks invented at MIT in the late 60's/early 70's."
http://www.cl.cam.ac.uk/~am21/hakmemc.html

Design of Line and Circle Algorithms (Powerpoint)
http://www.cs.stevens.edu/~quynh/courses/cs537-notes/lesson2-2DLines.ppt.

http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
 

Similar Topics

Anyone who can tell me please what's the relation between RMS ampere and FLA(full load ampere). Today I saw "RMS amperes" on AB servo motor's...
Replies
1
Views
2,295
Hi, I'm in the process of moving about 100 signals across from an old PLC system to a new CLX one. Some of the readings( flowmeters to begin...
Replies
18
Views
19,914
Ok I'm kinda stumped. Recent pet project is to draw/plot a circle with two axis control. The stumbling block lies in that there's no trig...
Replies
35
Views
9,830
Square-D Symax SFI-324 I have used the SFI-510 card for many projects, but I came across a SFI-324. Does anyone have ant tech info on it?
Replies
0
Views
78
hi everybody i want to make a backup (upload) from a plc square d micro 1 ready i have the software WINLDR i am looking the cable to Conect to a...
Replies
1
Views
517
Back
Top Bottom