24 Aug 2009

Calculator

Recently I was asked to complete our state machine designs presented on our web site by an example of an application controlling a user interface. The suggestion was to show an calculator design similar to the example of the Visual Basic calculator, as described in the book by Horrocks: “Constructing the User Interface with Statecharts” and also discussed by Samek in his more recent book: “Practical Statecharts in C/C++”. Samek does at least establish a strong case for basing such a design on a finite state machine, after discussing many defects of the Microsoft solution, but his design can and should be improved upon.

My first idea was just to repeat this example. Before I started I made a short analysis of the topic “calculator” discovering some interesting points. Using calculators I imagined that something like a standard user interface existed. I have been sometimes confused when getting into my hands a new calculator whose behavior is different to other ones. Anyway, I never worried much about this as my interest is limited to very simple operations. If my calculations are a bit longer or more complex I use spreadsheets.

Let’s then define the calculator to be designed. The simplest calculator possesses the keys:

  • to define the operands: digits (0..9), number sign (±) and decimal point (.)

  • to define the operations: add (+), subtract (-), multiply (*), divide (/), equals (=)

It has a display to show the operand typed in and the calculation result.

The few calculators I had at hand are similar but the use of the keys is not standardized:

  • the role of keys C and CE are interchanged; sometimes they get other names

  • the equals key (=) allows sometimes the calculation to be continued

  • the continuation of calculation after termination may be done using operation keys +-*/

The calculation scheme of Samek’s example adheres to the standard one: an actuation of an operation key (+-*/) is unique and defines the next operation, the following actuation of operation keys are ignored and the calculator awaits the operand. The limitation has been introduced to show that in such a case it is possible to use the key “-” in a double role: as an operation key (subtract) and as a sign of the operand. The other simplification is the priority of operations: they are performed in the sequence in which they are entered, let’s call it the “arithmetic for dummies”. It means that typing “1+2*3=” you get 9 instead of the expected 7. Maybe for people who terminated their education in the first class of the grammar school such calculations make sense but most of us respect the usual priority of arithmetic operations and such calculator would be rather useless for them. Imagine you want to calculate your shopping expenses: a piece of butter, 4 bottles of beer, 3 bars of chocolate, etc; no chances to do it with such a calculator (unless it has an add-to-memory feature of course). You find this kind of arithmetic in “simple” calculators. A curiosity of the Windows calculator is that in the Standard mode uses the “arithmetic for dummies” and if you switch to the Scientific mode it uses the arithmetic priority rules. Strange, the use of normal arithmetic is considered as a scientific approach! This limitation in the Samek example is obvious: to make the example as simple as possible, otherwise it will be too difficult. This is a artificial example, used to explain how to use statecharts in UML design. Obviously, UML statecharts does not work for any more practical problem.

As we prefer to work with examples taken from practice I had to decide which calculator from the few ones on my table and screen should be emulated. Eventually, I decided to design the calculator similar to one found on any computer running Windows.

Playing a little bit with this calculator I have found it has rather strange behavior in some situations. It is fairly easy to get the calculator to produce random results without any indication that the only way out is to reset it using the key “C”. For instance, trying “2/0+” you get expected “Cannot divide by 0″ but continuing with keys “CE” and “=” you get 10 (!). Maybe it is the code number of the software bug! Typing “2/0”= you get the same expected “Cannot divide by 0″ but continuing with keys “CE” and any new operand the calculator is in a loop generating “Cannot divide by 0″ which is in a way better. Of course the only solution is again the reset with the key “C”. Another example: “5+9/=” produces 10 which corresponds to “5+=” the pretended algorithm of using equals key “=”. But typing “5+9/CE=” produces an error message “Cannot divide by 0” (i.e. the clear key introduces 0 as an operand!). And now try “5+9/0=CE” and you get 100!

The calculator has another problems with unexpected sequences of operation keys. For instance, if you type “2+7*=” you get 4 (in Scientific mode). The Windows calculator manifests also a specific behavior after terminating the calculation with the equals key “=”. Repeating the key “=” performs the last operation using the result and the last operand; effectively it is the cash register function which is done normally using the key “+”. But if you change the operation and continue with the key “=” it continues using in the first step the last result as both operands. No idea what is this function for. I could not find any explanation of the described (faulty?) behavior in the voluminous Help which describes many obvious topics but does not explain how the calculator is to behave in the above described situations. In this situation the calculator ignores also entered numbers without informing the user about it. So if you type for instance”1+2=” you get 3. Entering then “4=” you get 5 which is OK in relation to the cash registration function but is confusing for the user who entered 4 having something else in mind. Investigating more carefully the behavior after entering the key “=” you will find also an algorithm that explains the strange result of previously shown operation “2+7*=”. This algorithm says: forget the last operator and operand and perform the cashier function. This might explain the behavior but what is this functionality for? Of course, the results in examples discussed in this section are different for Scientific and Standard modes. A possible use for this feature is to calculate compound interest: try 1.06 * = = = = = ; each = shows the effect of an extra 6% compound interest.

The calculator seems also to have difficulties with operation on and presentation of floating point numbers. By typing the sequence “9- sqrt =” you get 6 (as expected). By repeating the Equal sign you expect to get values decreasing by 3. It works but the 0 value is not correctly displayed. Namely the results are: 3, -3,46391…e-37, -3, -6, etc. I have not investigated this problem further as the numerical computation is not my favorite topic but it is a rather strange effect.

To get a more complete picture I have taken another calculator that I have in my drawer and repeated the same exercises getting different results but also partly erroneous!

I have to admit that after that experience I was a bit shocked. I have been convinced that a calculator is a mature product delivering correct results, not only for operations like 2+2. Obviously the software in these tested examples fails in “unexpected” situations. It is more or less a known problem in software implemented according to “use cases”, where the adequacy of the set of “cases” is left to the good will, time and ability of programmers.

After that introductory work I decided to take the problem seriously and design it with StateWORKS. I have been convinced that it is possible to get a calculator which behaves as required in all situations. I will present it in few steps. You find the discussion in the Case study section in a document “Calculator”.

...continue reading "Calculator"

Search tags: Tags: , , ,
Posted in General, stateworks

Posted by Ferdinand Wagner on 24 Aug 2009 at 14:34
Permalink | No Comments »

19 Mar 2009

Introducing “Questions”

In the category “Questions” you can ask any question about Virtual Finite State Machines or about how to use StateWORKS development tools. Simply post a comment to this entry.

...continue reading "Introducing “Questions”"

Search tags: Tags: , ,
Posted in Questions

Posted by Ferdinand Wagner on 19 Mar 2009 at 13:16
Permalink | 16 Comments »

23 Feb 2009

What is a state machine, again?

For many years we have been trying to popularize the correct concept of a finite state machine. In fact, we do not invent anything, we just present a state machine as defined ca. 60 years ago. In the beginning it had been applied to a design of hardware systems as software did not exist at that time. But the concept of a finite state machine is not bound in any way to the hardware; it is just a notation to describe a behavior and as such it is not bound to the physical implementation.

Read the rest of this entry »

...continue reading "What is a state machine, again?"

Search tags: Tags: , ,
Posted in Finite state machine

Posted by Ferdinand Wagner on 23 Feb 2009 at 20:28
Permalink | 4 Comments »

22 Nov 2008

Ours is probably the only industry which gets paid, first to do a bad job and then to keep fixing it

Recently we’ve received an email which characterizes the software profession as cited in the title.
That characterisation is not flattering, but may be appropriate. There are professions such as banking that seem to attract criminals, or should we say incurable optimists? We do not mean to imply that all bankers are criminals, but that this group contains more of them than other professions because “that is where the money is”.
Obviously, programmers does not belong to this category. To be a (good) programmer requires appropriate talent, some knowledge or even education and experience in several years of hard work. It is not very attractive even if we are relatively well paid. Unfortunately, in the eyes of our customers, clients or employers “we do a bad job and then keep on fixing it”.

Read the rest of this entry »

...continue reading "Ours is probably the only industry which gets paid, first to do a bad job and then to keep fixing it"

Search tags: Tags: , ,
Posted in General

Posted by Ferdinand Wagner on 22 Nov 2008 at 17:36
Permalink | No Comments »

18 Jul 2008

Using tcp/ip as an I/O interface

StateWORKS RTDB is a tcp/ip server. All attributes of each object in RTDB can be accessed by tcp/ip clients. A client connects to the RTDB server using two sockets: Request and Event. The Request (R) socket is used for queueing of object attributes. The Event (E) socket delivers object attributes that are registered as “advise” in RTDB.
The primary goal of the server/client model of the RTDB has been communication with a User Interface. When the tcp/ip communication began to be used for input/output purposes we could extend the StateWORKS server/client model very easily by completing attributes of corresponding object by a concept of RawData. The completion of Raw data are required for objects: DI, DO, NI, NO, the objects CMD, XDA, PAR and DAT possessing per se the raw data values.
We have add another technical note to our web site describing the tcp/ip communication in RTDB. You may download the note http://www.stateworks.com/active/download/TN24-Using-tcpip-as-io.pdf to study the details.

Read the rest of this entry »

...continue reading "Using tcp/ip as an I/O interface"

Search tags: Tags: , , , ,
Posted in stateworks

Posted by Ferdinand Wagner on 18 Jul 2008 at 12:09
Permalink | No Comments »

7 Jul 2008

Getting started

I received an email from John M. who is evaluating StateWORKS. He started with a fairly complex example that has confronted him with several issues that must be understood to use effectively our tools and the run-time system. Because of a general nature of his questions I put (with his acceptance) my answers into that blog.

Read the rest of this entry »

...continue reading "Getting started"

Search tags: Tags: , , , ,
Posted in stateworks

Posted by Ferdinand Wagner on 7 Jul 2008 at 12:38
Permalink | 1 Comment »

11 Mar 2008

Book critics

Our book “Modeling software with Finite State Machines” has received quite good reviews. Recently, I read in Amazon two controversial reviews on which I commented to my friends in an email as follows:

Read the rest of this entry »

...continue reading "Book critics"

Search tags: Tags: ,
Posted in General

Posted by Ferdinand Wagner on 11 Mar 2008 at 10:08
Permalink | No Comments »

3 Mar 2008

What is a state machine?

Our web site contains several technical notes and papers that discuss some aspects of software engineering associated with the concept of a finite state machine. On introducing our blog we have decided to present you a few topics covered by a group of our documents. We started with the Mealy and Moore models of a finite state machine, honoring the fact that it is a favorite topic according to visitors’ statistics. The topic that we would like to present you today is “What is a state machine”.

Read the rest of this entry »

...continue reading "What is a state machine?"

Search tags: Tags: , , , ,
Posted in Finite state machine

Posted by Ferdinand Wagner on 3 Mar 2008 at 08:00
Permalink | 5 Comments »

22 Feb 2008

Moore or Mealy model

Moore and Mealy models are the basic two models of state machines as defined in the literature. To understand the differences between them the actions (outputs) must be defined and bound to state changes. The technical note “Moore or Mealy model?” discusses both models and shows that the optimal solution is achieved when features of both models are used in a mixed model. Although the concepts of these two models are useful when performing certain theoretical studies, they are also interesting for software developers, and StateWORKS makes the use of a mixed model easy and effective.

Read the rest of this entry »

...continue reading "Moore or Mealy model"

Search tags: Tags: , , ,
Posted in Finite state machine

Posted by Ferdinand Wagner on 22 Feb 2008 at 17:58
Permalink | No Comments »