COSINE with Micrologix1500


Thread Starter

José Claudeni

I have to do a application with Micrologix1500 that need to get the cosine of an angle in degrees (Everybody knows: Micrologix don't have trigonometrics instructions). This angle is a interger data (-32 to 72). What options do I
have? Tables? Taylor's Serie approximation?
I would use a lookup table if possible. If your
angle resolution is only 1 degrees you would have 100 entries.
Since your angle is integer data a table would be a good approach. I'm assuming your using float data for your table. Create a file such
as F8 with 105 elements and punch in the cosine data. Put the cosine(-32) at F8:0 thru cosine(72) at N7:104. A bit tedious but it should
only take 20 minutes or so to do. The rest is super simple. Use indirect addressing to look up your value in the table. Assuming that your
angle is at N7:0, Perform the following instruction:

SUB N7:0 -32 N7:1

Your cosine of the angle is at F8:[N7:1]

CAUTION, its very bad to indirect address beyond the end of a file, so range check either your angle (N7:0) or your result (N7:1) to prevent
that from happening if there is any possibility that the angle can be something outside the range you gave.
One more thing, be sure to mark the data file as static so that the values in it are not accidentally changed.
Tables are easiest to get right, and also faster than anything else, which may be a benefit in itself.

Taylor series (or similar) approximation is the other way, especially if you don't need high accuracy, but I'd be tempted not to bother (unless memory is tight, and maybe even then - code takes up memory too).

Tables have the additional advantage that it's much easier to modify them if the definition of cosine ever changes. Which sounds silly, but also
likely, in that maintenance might find that it wasn't quite the function that best does the job...

Jiri Baum <[email protected]>
MAT LinuxPLC project --- --- Machine Automation Tools
The MicroLogix doesn't do Floating Point math. If you would like, I can send a sample of how to convert integers to floating points. It is rather involved, so depending on the scope of your project, you may wish to consider upgrading to a different processor.

Bob Peterson

RSLogix 500 allows floating point in a series C Micrologix 1500. But it does not allow any of the more advanced math functions such as cosine.

You could use a lookup table with interpolation for this, but it might be more trouble then its worth.

What is it you are trying to do?

Bob Peterson

Jacek Dobrowolski

I'd try to prepare a table (as you need only 105 values - if I understood well). Otherwise heavy load on the plc scan time according to calculations.


Jacek Dobrowolski

Although I have not used this controller, I have had to solve this problem before for real-time control using four function math. Here's what I would suggest:

1) First, determine the precision of the result that you need, i. e. how many decimal places for the cosine final result?

2) Next, determine if there are speed or other constraints that force you to solve the problem in a specific way (program memory words? table size?).

3) Next, take a look at your options for solving, for example, Taylor Series, Chebychev polynomials, trig addition, etc.

Without knowing more, I would say that you don't need expansion or polynomials; my guess is that you could do something like this as the easiest and fastest way:

Use the trig addition formula:

cos( a + b ) = cos a * cos b - sin a * sin b

1. Set up tables, for example if you only need cos( angle ) to one degree, you need two sets of sine and cosine tables for a 105 degree span.

A. Set up ten's table, sine and cosine for 0, 10, 20, 30, 40, 50.....100 degrees (this could be offset by +32 to make it easier); 22 table entries.

B. Set up one's table, sine and cosine for 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 degrees; 20 table entries.

2. Break the number into its tens and ones components, look up the sine and cosine for each component, and do the math for trig addition; only two multiplies and one subtract.

3. If you need accuracy to one-tenth degree, you can do a straightline calc between the two closest integers and it would be pretty close.

This is fast, doesn't need much memory, and should be fairly understandable.

If this doesn't get you where you need to be, post more information about the problem and I'll get back to you.

One other mention about doing this - once the program is written, the range of values is small enough that I would write a test program that runs
through all possible values, just to check the output. This will validate the software design for any problems that could occur from unexpected
input, limit values, etc. It would be good to test the input range for validity before solving the equations, also....even if "this could never
happen while running".


Willy Smith
Numatics, Inc.
Costa Rica


All MicroLogix1200 and MicroLogix1500 controllers can be flash upgraded to Series C, which supports floating point and establishes default F8.
If your controller is a lower series, get the flash upgrade at

Hope this helps!

Larry Lawver
Rexel / Central Florida

P.S. Great comments by all responding to this thread! I endorse the table lookup method as well.

Ranjan Acharya

With regards to calculations of any trigonometric or transcends function within any type of computer, a Taylor Series approximation is rarely used. Such an approximation requires many iterations. The secret is to calculate the result in one or perhaps two passes of an algorithm.

I once performed such operations on an Intel 8096 using routines in the book "Software Manual for the Elementary Functions" by William Cody and William Waite. Their routines are a little bit dated - there are more accurate ones used in modern mathematics co-processors or floating point streams.

Cody and Waite will provide you with both fixed point and floating point algorithms. They are actually quite interesting - I always thought that it was Taylor Series too.