Sunday, 1 December 2013

The careless mistakes that programmers make

This week has been a busy one, and I do wonder if my sexier-than-ever voice now is a sign that I should take a break. In any case, today I would like to share an "algorithm" I saw at the workplace. It is gentle reminder to always test your code...

The purpose of this algorithm is to slowly open (or close) a valve, i.e. ramping. Generally, it is desirable to change valve openings in an incremental and steady manner to avoid a sudden disturbance (e.g. a huge increase in flow) to the process. This algorithm takes as its user input the target opening, the time given to reach the target opening, and the time interval between each move. Now, study the flow chart below. Can you find out the subtle error?




Initial opening (A)
Target opening (B)
Current opening (x)
Time allocated to reach target opening (T)
Time interval between moves (t)


                     start
                       |
                       |
                     A = x
                       |
                       |
         Compute move size m = (B-x)*(t/T)  <--------
                       |                             |
                       |                             |
                     B > A ?                         |
                       |                             |            
          _____yes_____|_______no___                 |
         |                          |                |
         |                          |                |
         |                          |                |
         |            yes           |                |
 --  x + m >= B ?  ->-- --<-  x + m <= B ?  ----     |
 |                     |                        |    |
 |                     |                        |    |
 |                     |                        |   wait t
 |                   x = B                      |    |
 |          (This to avoid overstep)            |    |
 |                     |                        |    |
 |no                   |                      no|    |
 |                     |                        |    |
 |                    end                       |    |
 |                                              |    |
 |                                              |    |
 |                                              |    |
 |                                              |    |
  -------------->  x = x + m  <-----------------     |
                       |                             |
                       |                             |
                       |_____________________________|





Where's the reward? I hear some of you ask. Well, nothing tangible, just the satisfaction of debugging... Happy weekends! *cough* *sniff* *sniff*

No comments:

Post a Comment