Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/19/06 18:45
Read: times


 
#124607 - Calculation vs Data Structures vs Logic
Responding to: ???'s previous message
OK I found the book and refreshed my memory. It's nearly 20 years since I first read this but fortunately the book is now available on line - unfortunately for most folk here it is all about Forth.

Anyway, the bit that stuck in my mind was this:

Calculation vs DataStructure vs Logic

We've stated before that the best solution to a problem is the simplest adequate one; for any problem we should strive for the simplest approach. Suppose we must write code to fulfill the following specification:

if the input argument is 1, the output is 10
if the input argument is 2, the output is 12
if the input argument is 3, the output is 14

There are three aproaches we could take:

1.Calculation:

out = 10 + ((in - 1) * 2)

2. Data Structure

int anarray [] {10,12,14};
out = anarray[in -1];

3. Logic

switch(in) {
    case 0: out = 10; break;
    case 1: out = 12; break;
    case 2: out = 14; break;

}

In this problem, calculation is simplest. Assuming it is also adequate (speed is not critical), calculation is best.

The problem of converting angles to sines and cosines can be implemented more simply (at least in terms of lines of code and object size) by calcualting the answer than by using a data structure. But for many application requiring trig, it's faster to look up the answer in a table stored in memory. In this case, the simplest adequate solution is using the data structure.



The book then goes on to give other examples to illustrate the differences between the approaches and concludes:

In choosing which approach to apply towards solving a problem, give preference in the following order:

1. Calculation (except when speed counts)
2. Data structure
3. Logic

This is a (classic) book on Forth which is a real time control language (available for the 8051) so I have changed the code examples in the quote but it is pretty relevant to 8051 applications (I first used Forth in 1987 for an ATM development).

This is the only book I have ever found that has made the distinction between the three approaches let alone expressed a preferred order.

Hope that clarifies things. Anyone interested in the book can download it in pdf form from http://thinking-forth.sourceforge.net/

Ian

List of 13 messages in thread
TopicAuthorDate
Logic or Algorithm            01/01/70 00:00      
   So what was your "Algorithm"?            01/01/70 00:00      
      back to Hurley            01/01/70 00:00      
         Programming 101            01/01/70 00:00      
            sometimes it's not bad to remember history            01/01/70 00:00      
      My Algorithm            01/01/70 00:00      
         Lost            01/01/70 00:00      
            Think think think .....            01/01/70 00:00      
               Hurley again            01/01/70 00:00      
   well only today            01/01/70 00:00      
   Is there a difference?            01/01/70 00:00      
      Terminology - Ian?            01/01/70 00:00      
         Calculation vs Data Structures vs Logic            01/01/70 00:00      

Back to Subject List