More Comparing Calculation Tools

Before continuing with Building a Structural Engineering Design eWorkbook, I thought I would backtrack and extend the comparison of calculation tools.  In the first article I compared some  computer based calculators,  Computer Algebra Systems (CAS) and simple scripting languages.

  1. ATCalc
  2. Calc98
  3. SMath (cf. MathCAD)
  4. FreeMat (cf. MatLAB)
  5. Python
  6. VBScript (WSH)
  7. JScript (WSH)
  8. FreeBasic
  9. LibreOffice Calc / LOB
  10. MS Excel / VBA

My conclusion was that I saw no benefit in changing from MS Excel/VBA. Since FreeMat basically moves us into using interpreted scripting languages, may as well also take a look at full programming languages.

Background to Programming and Using Computers

My first introduction to computers was likely a HP calculator that my dad borrowed from work, it had small magnetic strip cards, when my dad wasn’t using occasionally got to play a game on the calculator: it used the numbers as letters when the display turned upside down: this was around 1974.

After that aware of computers because dad supplied 132 character old printouts to school for scrap paper. Then at school around grade 7 (1977), we used pencil to marked input cards, using BASIC, which were sent off to a computer bureau, a few weeks later we all got different calendars back, with pictures drawn using ASCII text. All a mystery and not very useful.

Next use of computers was grade 10 (1980), this time a quick introduction to APL, which we then used to mark up cards to solve some matrix problems, the cards once again sent off to a bureau and a few weeks later got the results back. Interesting but clearly computers not very practical for solving problems as it took far too long.

Got to university and more formal use of computers, using a VAX main frame with VMS operating system, everything was explained in a strange manner concerning areas and didn’t make much sense. The programming language we started with was Fortran 77, we marked up code sheets passed on to data operators who punched into the computers. The subject was part of engineering mathematics and the textbook was: Daniel D McCracken (1984), “Computing for Engineers and Scientists with Fortran 77”, John Wiley & Sons.

Once the program was in the computer we were then allowed to run, and edit from a terminal. We were taught structured programming, requiring the extensive use of comments to identify various standardised branching structures such as while loops and repeat loops, which were not supported by the language. We were only permitted to use goto statements as part of such properly defined and commented structures. After Fortran we had a quick introduction to BASIC.

Not long afterwards got a Microbee computer at home, with CPM/80 operating system, once I learnt about User areas, the past lectures on VMS started to make more sense. The only programming language initially available on the computer was a dialect of BASIC, so I started experimenting with what was available, though I would have preferred to have Fortran. Then we got Turbo Pascal, and the only thing I missed from Fortran was the exponentiation operator (**).  We also had WordStar, SpellStar and MailMerge: I had this idea of writing reports in WordStar and using Turbo Pascal to do the number crunching and save results to Mailmerge data files which could then be merged with wordstar reports. This would save the hassle of trying to format printout reports with Pascal, as  wordstar was already configured to use fancy features of the printer, so didn’t have to mess around with printers (even if epson control characters were a de facto standard).

Then I was introduced to spreadsheets (Multiplan), and databases (DBase), my idea for wordstar became redundant. We got a Microbyte computer running MSDOS, we also got “as easy as” spreadsheet, then Borland Quattro Pro. Upgraded to Turbo Pascal 5.5 (TP), started to learn object oriented programming, also got Turbo C (TC). I had also returned to university studying business, where I got status in subjects covering Lotus 123, MS Word, and DBase IV as I could already use (well except word and I did the tutorial on that whilst waiting for tutor to ask for status). At work I used FoxBase. For home I got Borland Paradox database management system (DBMS) as it was lower cost than DBase. I liked the Paradox application language (PAL), it was like a combination of Pascal and C, but I preferred the DBase/FoxBase command line interface compared to the Lotus 123 type menu interface of Paradox.

The benefit of the DBMS is that reading and writing datafiles is easy, and so is creating input forms and generating reports. With Pascal/C have to write a file parser for a text file if use commas and quotes to delimit the data, if use fixed width file format get problems with strings and numbers on the same line. (cf. with BASIC where some versions can read comma delimited files, no need to write a parser as its built in.)

At the time it was important to pick between using a spreadsheet or DBMS, as spreadsheets required enough RAM to hold model in memory whilst databases worked on files. For example at one place I worked the RAM was removed from several computers to boost memory available on my computer for Lotus 123, so that I could transform railway curve data into some other form required by the civil engineer. I suggested it would be better to use FoxBase as that was available, but didn’t have adequate number of licenses nor did the engineer no how to use. I consider having used inappropriate software for the task that the solution was less than robust: not the least of which the civil engineer needed more RAM for his computer, and the RAM in mine computer needed handing back. There were too many conditional tests in the worksheet cells, I exceeded the length of formula allowed and had to create numerous flags to check, and the formula weren’t necessarily consistent has moved vertically through the data. With a DBMS it would have been one set of conditions written once, and applied consistently to all records and no additional RAM would have been required.

Since Windows not so important a choice, as both spreadsheets and DBMS make use of virtual memory and swap files. Though there are still situations where using a DBMS like MS Access is preferable to using a spreadsheet like MS Excel. A DBMS can simplify programming compared to using languages like TP/TC, as file format already determined, and screen input forms are simplified and so is reporting. However TP/TC were important with respect to working with other programs such as AutoCAD, MicroStran. Whilst AutoCAD had its own command language, application scripting languages (menu macros, DIESEL, AutoLISP), getting information to and from AutoCAD was/is primarily via DXF files, which have to be processed with something: that something is typically a program written in a high level language.

When moved to Windows moved onto Borland Delphi and Borland C++ Builder, also had Visual Basic but never really used. Trapped into nonsense that C/C++ were the languages which should be learning and using for future AutoCAD development, and that BASIC wasn’t a real language.

But things changed. Using Quattro Pro for Windows macro language was able to rewrite some of Pascal programs and C programs. Plus primary task was presenting calculations, and creating dialogue boxes and collecting data was easy to do in QPro. I was doing some parallel developments in Delphi, but QPro was the easy place to collect data, and present reports. Whilst input/output to Delphi calculations were more difficult and more time consuming in getting the presentation just right. Then time came that I wanted to write functions in QPro, but that wasn’t easy via dynamic link libraries. Then one day wandering round book store during lunch break I found a book on Office 97 and VBA programming. After buying and reading I went and got Office 97 and Systems Development Kit. I may not have liked BASIC, but VBA made BASIC similar to Pascal, and it was a common language across applications: which was better than existing situation where I was using one macro language for Wordperfect, another for Quattro Pro and another for Paradox. Now I used the same language for Word, Excel and Access. I started to translate my Delphi/Pascal code into VBA, and also create similar dialogue boxes in MS Excel.

Learning different applications and different application programming languages and high level languages isn’t really a problem its just inefficient. It is also not exactly correct that C programs are faster. Programs in C are only faster if the compiler can produce fast machine code, if other languages produce slower code then they need better compilers. If can translate high level language to machine code, then authors of compilers, should be also capable of translating between languages. The programming language a program is written in should be irrelevant. Also whilst syntax is a major difference between languages, the more influential difference is the availability of libraries either built into the language or available as extensions. Fortran was important because of the IMSL library, but for many things the library not that much of a benefit and more efficient to write something more specific than use a library routine. Also most people studying programming would create subroutines for some of the common tasks, and therefore also have no need for available libraries: unless they need optimized algorithms.

So in the past I choose applications and languages based on which would enable me to get the desired solution fastest, not which would produce the fastest running program or need the smallest storage space. I was introduced to spreadsheets around 1987, started using them regularly around 1989, and started with MS Excel in 1998. So having been using VBA since 1998, I have built up a lot of code to automate various tasks: rewriting wouldn’t be overly productive. On the other hand converting code which concerns crunching numbers is not that difficult to do from one language to another. Conversion typically just requires carrying out multiple global replacements in the correct sequence and then some manual tidy up. The global replacements can be made using a scripting language, that’s how I translated the Pascal/VBA versions of our plane frame program into various languages, by writing scripts in VBScript and using regular expressions.

On With the Comparison

So the primary task is to choose a language which makes the task relatively easy to do and quick to do, we primarily want to get answers not to spend time programming. Our primary requirement is typically to evaluate a sequence of relatively simple calculations. The example for the current situation is the simple sequence as shown in the following SMath display.

Simple Structural Calculation: Beam Bending Moment from Wind Load
Simple Structural Calculation: Beam Bending Moment from Wind Load

In the original calculation sequence there was no variable ‘s’, the value ‘3’ was input directly into the calculation of ‘pn’, and this approach has been replicated in some of the programmed versions which follow. The calculation sequence is relatively trivial, and a real set of structural calculations would comprise of considerably more expressions. For the comparison the following additional languages were employed:

  1. Ada
  2. AutoLISP
  3. C
  4. C#
  5. Fortran
  6. Java
  7. Lazarus/Free Pascal (cf Delphi)
  8. perl
  9. php
  10. Powershell
  11. Tcl
  12. VB.net

The list is just alphabetical, not the order they were tested nor any ordering of preference.

Since primary activity involves Excel/VBA and VBScript, the next logical choice would be VB. When I made this leap and got visual studio (VS) 2003 with VB only, the language had changed to VB.net, since there were two of us in the office a short time later got VS 2005 with all supported languages. If using arrays then VBA to VB can be a problem as VB only supports zero based arrays, as do most of the others languages selected for the comparison: that was the main obstacle to converting our plane frame program. Anycase my current laptop is 64 bit Windows 10, and my VS 2005 license didn’t install properly so used SharpDevelop instead for both VB.net and C#. (Visual Studio Express is too slow to startup and internet connected.)

Module Program
    Sub Main()  
        Dim Cpe As Double
        Dim qz As Double
        Dim pn As Double 
        Dim s As Double
        Dim w As Double 
        Dim L As Double
        Dim M As Double         
        
        Cpe = -0.7
        qz = 0.96           'kPa
        pn = Cpe * qz       'kPa
        s = 3           'm
        w = pn * s          'kN/m
        L = 6               'm
        M = w * L ^ 2 / 8   'kNm
        
        Console.WriteLine("Moment: " & Format(M, "0.00"))

    End Sub
End Module
VB.net

VB.Net When the program is run, we just get to see the result, no display of the inputs, and there is no opportunity to change the parameters. This will be the same for all the examples, if want to use different parameters to get different result then need to write, compile and run a different program.

Extra work is required to get information into the program, store inputs and read inputs from the store, and format a report presenting: inputs, outputs and the formula used in the calculation process. So whilst information is there in the program it doesn’t display in any output report, unless such report is coded: such coding requires duplication of content with possible transcription errors, such that the report doesn’t necessarily match the calculations.


using System;

namespace StructuralCalcs
{
    class Program
    {
        public static void Main(string[] args)
        {
        double Cpe;
        double qz;
        double pn;
        double w;
        double L;
        double M;
                        
                Cpe=-0.7;
        qz=0.96;                //kPa
        pn=Cpe*qz;              //kPa
        w=pn*3.000;                     //kN/m
        L=6.000;                //m
        M=w*Math.Pow(L,2)/8;                    //kNm

        Console.WriteLine("Moment " + M.ToString("0.00").PadLeft(8) + " kNm"  );
            
        }
    }
}
 C#

C# near enough similar to  the VB.net, though no operator for exponentiation but otherwise have the Math.Pow function available. Not having an operator for exponentiation is common across languages, for the current task simply repeating the variable in multiplication is simpler than looking up a power function: in some of the code I have used the ‘pow’ function simply to show it is available in the language, in other cases I couldn’t be bothered I had enough problems setting up the compilers. {Note had to add decimal places to some of the numbers}

For both VB.net and C# whilst the main program code is in a single file, the development environments create a lot of litter: both files and folders. So compared to the previous scripting languages the current batch of languages are messy.


program StructuralCalc;

Var
  Cpe : double;
  qz : double;
  pn : double;
  w : double;
  L : double;
  M : double;

begin

Cpe:=-0.7;
qz:=0.96;       {kPa}
pn:=Cpe*qz;     {kPa}
w:=pn*3;        {kN/m}
L:=6;           {m}
M:=w*L*L/8;     {kNm}
writeln('Moment: ', M:4:2);

end.
 Lazarus/Pascal

As with VB.net, Lazarus also creates a lot of litter. However I also converted the main file to .pas and using FreeDOS compiled with Turbo Pascal 5.5. Probably also a lot less litter if I worked from command line using Free Pascal directly rather than through Lazarus. However Lazarus provides similar capability to Delphi and so can write more comprehensive program: have one version of plane frame analysis program with graphics written in Lazarus, Delphi and VB.net. {Lazarus lacks support for MDI and so that version of plane frame needs rewriting: so whilst language may not change features provided by a library may disappear. In this case authors of Lazarus didn’t write support for MDI in first place and have no intention of providing in future, whilst indications are that other development environments are likely to lose such feature.}


#include 
#include 
int main()
{
    float Cpe,qz,pn,w,L,M;

    Cpe=-0.7;
    qz=0.96;            //kPa
    pn=Cpe*qz;          //kPa
    w=pn*3.000;         //kN/m
    L=6.000;            //m
    M=w*pow(L,2)/8;     //kNm

    printf("Moment: %5.2f" , M);
}
 C

C language program gave me the most problems to compile. I started off with Bloodshed C, and didn’t get anything on 64 bit Windows 10. So I then installed Code Blocks, which turned out not to have compilers installed with it. I installed mingw-w64 and setup, but coldblocks still complained about couldn’t find gnu compiler, so I installed mingw. None of it seemed to work. I also installed Microsoft C++ Tools, never got round to setting this up in code blocks or otherwise testing.

I got the program compiled on Windows XP computer using Bloodshed C, I also used mingw gcc from command line and got the program compiled. Tried again on Windows 10, using command line, discovered Norton deleting the a.exe file as soon as compiled. Furthermore as soon as I inserted memory stick with compiled version of program, Norton deleted from memory stick: not impressed: apparently a common problem. Backtracking to FreeDOS and Turbo C V2, compiled with minimum problem, just an issue with environment directories: a typing error from when I originally set it up about 2 years back but never tested.

Anycase the C code is relatively shorter than that in the preceding languages, but then again the Pascal code could be shorter as not necessary to list each variable on a separate line, can declare all variables in single statement as with C.

I was actually surprised that Turbo Pascal compiled the code, as I wasn’t aware it understood doubles, and otherwise used user defined types for floats, as I recollect. The more compact nature of TC is why I tended to prefer it over TP for number crunching, though its use of pointers for arrays and strings could cause problems. Though strings always cause problems in data files. Whilst messing with pointers was useful for string manipulation, the use of pointers when working with records and no ‘with’ statement made C somewhat difficult to read, resulting in a preference for TP for such uses: plus TP5 was object oriented whilst TC wasn’t. On moving to Windows, Delphi was good and easy to use, whilst C++ Builder was always having configuration problems, and failed to find its own library files but flagged errors with my code. So I could compile examples, but had difficulty compiling “hello world”, so avoided C++. Hence I haven’t provided an example here. C++ would mostly be the same as the C code but with different statements for printout, and therefore different include files.


      PROGRAM StructuralCalc
      REAL Cpe,qz,pn,w,L,M
      Cpe=-0.7
      qz=0.96
      pn=Cpe*qz
      w=pn*3
      L=6
      M=w*L**2/8
      PRINT '(A)','Moment: ', M
      STOP
      END

 Fortran

I experienced similar problems with Fortran has I had with C, as was likewise using the gnu compiler collection (gfortran). As with C, Norton kept deleting the compiled file. But worked ok on Windows XP, with expired virus protection (Trend Micro). Not sure if modern Fortran is still column dependent, but I otherwise followed rules of Fortran 77. Relatively short and simple and formatting the output can be improved: I just didn’t get that involved as main task is to crunch the numbers.


with 
    ada.Text_IO,
    ada.long_float_text_io;


procedure StructuralCalcs is

  Cpe : long_float;
  qz : long_float;
  pn : long_float;
  s : long_float;
  w : long_float;
  L : long_float;
  M : long_float;

begin

    Cpe:=-0.7;
    qz:=0.96;       --kPa
    pn:=Cpe*qz;     --kPa
    s:=3.0;         --m
    w:=pn*s;        --kN/m
    L:=6.0;         --m
    M:=w*L*L/8.0;       --kNm
    Ada.Text_IO.Put("Moment: ");
    Ada.long_float_text_io.Put(M,5,2,0);
    Ada.Text_IO.Put(" kNm");


end StructuralCalcs;

 Ada

For the Ada programming language I used GPS (gnat programming studio), I had some initial problems getting to compile, until started loading the project file first. Apparently Ada not big on input/output as its original purpose was embedded systems and machine control (eg. defence systems , civil systems (eg. traffic lights)), so file writing routines a bit clumsy: though they could be built upon if used regularly. There is some potential to simplify some of the code so that only need reference to the word ‘Put’ without all the qualifying prefixes: it just requires the appropriate statements at the top of the code.


(defun c:StructCalc ( / Cpe qz pn w L M)
    (setq
        Cpe -0.7
        qz 0.96
        pn (* Cpe qz)
        w (* pn 3)
        L 6
        M  ( / (* w (* L L)) 8)
    )
    (princ (strcat "Moment " (rtos M) " kNm") )
)

 LISP

I attempted to install LISP on my computer but there was some indication of something missing, so abandoned that approach, and so settled for familiar environment of AutoLISP or IntelliCAD LISP as available in ProgeCAD 2016.

Whilst I am familiar with Reverse Polish Notation (RPN) calculators and wouldn’t choose any other kind of calculator, and have programmed HP28S using its RPN language, using prefix or postfix notation, to write formulas is not overly convenient. Compared to other languages LISP requires significant rewriting of the formula.

In the past I have experimented with Forth  (as I recollect an RPN based language) and Turbo Prolog (another language like LISP used for artificial intelligence research and expert systems). I was going to add these languages to the comparison but didn’t seem all that convenient to set up development environments for these languages.

Prolog not really for calculations, though Forth may be useful. The main reason to include LISP is that AutoCAD and derivative CAD packages use LISP for programming access to the drawing database. But unlikely to want to use LISP for some of the more complex formula involved in structural design: as I illustrated in  my first comparison of calculation software: Electronic Calculations (eCalc’s) the formula can be more involved than currently using.

Both AutoCAD and ProgeCAD support COM automation and can be programmed from Excel/VBA or by any language which supports COM automation. So AutoLISP not all that important for calculations, its main importance is for interaction with the AutoCAD/ProgeCAD editor and such interaction is an inefficient way to automate: if truly automating then human interaction needs to be removed. In short the importance of LISP’s role in CADD development is diminishing, as it has limited ability to interact with other applications. Why write a LISP routine which only works in AutoCAD, when can write a VBA routine which will work with other CADD packages once change the library objects to suit (eg. IntelliCAD, TurboCAD, DesignCAD, Excel Shapes and DXF processing)?


public class StructuralCalcs {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        double Cpe;
        double qz;
        double pn;
        double w;
        double L;
        double M;

        Cpe=-0.7;
        qz=0.96;            //kPa
        pn=Cpe*qz;          //kPa
        w=pn*3.000;             //kN/m
        L=6.000;            //m
        M=w*Math.pow(L,2)/8;            //kNm   
        
        System.out.format("Moment %8.2f kNm%n", M);
    }
    
}
 Java

The above code is Java, I started with Netbeans as my memory was: it was easier than Eclipse to get to something that could be run. It wasn’t the case so I ended up working in both Netbeans and Eclipse. As with VB.net beside the main source file, a lot of litter generated. Running Java files not so easy.


my $Cpe=-0.7;
my $qz=0.96;        #kPa
my $pn=$Cpe*$qz;    #kPa
my $w=$pn*3;        #kN/m
my $L=6;        #m
my $M=$w*$L**2/8;   #kNm
printf( "Moment: %.2f kNm" , $M );
 Pearl

The above is pearl, relatively simple to use, only problem I have is I don’t like languages which use ‘$’ symbols to prefix or postfix variables: including old versions of BASIC.


$Cpe=-0.7;
$qz=0.96;       //kPa
$pn=$Cpe*$qz;       //kPa
$w=$pn*3;       //kN/m
$L=6;           //m
$M=$w*pow($L,2)/8;  //kNm
echo "Moment: " . $M . " kNm";
 php

The above is php, I figured I’d check it out as wordpress is built using php, and since it is I have left out the php tags which wrap the whole code (wordpress didn’t seem to like in the post). Otherwise easy to write and run.


$Cpe=-0.7
$qz=0.96        #kPa
$pn=$Cpe*$qz        #kPa
$w=$pn*3        #kN/m
$L=6            #m
$M=$w*[system.math]::pow($L,2)/8     #kNm
Write-Host "Moment: $M kNm"
 Powershell

Since using windows and already covered windows scripting host (WSH), and the use of VBScript and JScript, thought may as well test their purported replacement: Powershell. Wasn’t expecting much as not made for number crunching, it’s designed for manipulating system objects. However it has access to the .net framework and so can make use of the math library, though its reference to such library is cumbersome. As a language using ‘$’ prefixes I don’t like.


set Cpe -0.7
set qz 0.96
set pn [expr {$Cpe*$qz}]
set w [expr {$pn*3.0}]
set L 6.0   
set M [expr {$w*$L*$L/8.0}]
puts "Moment: $M kNm"
 Tcl

The last language to try was Tcl. This is a scripting language, as I understand for meshing applications together and building interfaces. So didn’t expect much in the way of number crunching, and its approach is somewhat cumbersome.

Summary

All up the two posts have covered around 22 applications and languages. It still suggests that Excel/VBA is still the way to go. The VBA can easily be converted into VBScript as the main requirement is to delete variable types from ‘Dim’ statements. Whilst VBScript can largely be copied to VBA unchanged, other than maybe need to change ‘echo’ statements to ‘debug.print’ statements. Adding types to ‘Dim’ statements in VBA is optional. Translating to VB.net is slightly more involved. In the main, VBA can be copied directly to VB.net without change, especially if adopt some simple constraints when using VBA (eg. only use zero based arrays).

Translating the VBA to other languages such as Pascal, C#, Java isn’t too great a problem. These other languages are only of benefit if they provide something not available through Excel/VBA. These other benefits maybe in terms of libraries which are used for: mathematics, building user interfaces, reading and writing various file formats, and generating reports.

As indicated at the beginning of the post none of the example code involves, getting inputs from the user, there is no data storage or retrieval, and there is no complete reporting. There is just the simple task of crunching the required numbers and displaying the required result. If the calculation is carried out in an Excel worksheet, then we get input forms, data storage and retrieval, and reporting as complete as we need. A VBA application can work with the worksheet, and so such application, also has input forms, data storage and retrieval, and reporting without too much additional effort. Additionally a VBA application can use MS Forms to create dialog boxes to collect data.

In short MS Excel and MS Access take the place of libraries and frameworks which likely need if programming in say C# or python.  The office applications provide for rapid application development, and the number crunching can be relatively easily translated to other languages. The difficult part of conversion from Excel/VBA would be building an application with similar features to MS Excel/Access from scratch, in say C# : and that difficulty is why wouldn’t build the application using C# in the first place. My primary task is to crunch numbers (actually it’s to know what the numbers mean and make decisions based on), not be a wiz at building sophisticated user interfaces. Whilst building interfaces can be interesting, it otherwise gets me sidetracked from, and delays me reaching the decisions I need to make based on numbers.

In the previous comparison, whilst I mentioned the VBA editor (VBE) immediate window, I didn’t illustrate its use. The use of the VBE immediate window for our reference calculation sequence is as follows:

Example calculation process using the VBE immediate window Excel/VBA
Example calculation process using the VBE immediate window Excel/VBA

So don’t need FreeMat, or Python with Idle, and I don’t need to write VBA code modules, I can just write VBA code and discard it. So all up MS Excel/VBA is a substantial and versatile tool set. So whilst it may be considered cooler to program in C, or to consider that “Real Programmers Program in Fortran”, these languages and their available development environments do not provide the most suitable and versatile tools required for the task at hand, and that is routine number crunching and data transformation.

So Excel/VBA is the tool I will stick with, and the next step of migration if and only if necessary would be VB.net and associated Visual Studio Tools for Office (VSTO).  Also if LibreOffice improves its Basic development environment, may consider moving over to LibreOffice.

But until then I will be illustrating building a structural engineering eWorkbook using MS Excel/VBA.


Revisions:

  1. [09/10/2018]: Original (first draft)
  2. [10/10/2018]: Added some headings, rewrote some paragraphs.