Tag Archives: mathematics

How units work

In this post, we will take a quick look inside Calcpad and see how units actually work. But first, let’s make a brief overview of the theory basis.

Theory basis

There are only seven base units in the whole universe. They correspond to the seven physical dimensions:Measurement

  • mass – kilogram (kg)
  • length – meter (m)
  • time – second (s)
  • electric current – ampere (A)
  • temperature – Kelvin (K)
  • amount of substance – mole (mol)
  • luminous intensity – candela (cd)

All other units are derived from the above ones, using the respective laws of physics. For example, forces are measured in Newtons (N). By definition, force is mass × acceleration, so 1 N = 1 kg·m/s2. Such units are called “composite”. They can be always decomposed to their constituent units.

Other method to derive units is by adding prefixes. I this way, we can obtain multiples of both base and composite units. For example,  kilonewtons – kN = 103·N, meganewtons – MN = kN = 106·N and so on. All possible prefixes are listed in the following tables:

Name deca hecto kilo mega giga tera peta exa zetta yotta
Symbol da h k M G T P E Z Y
Factor 101 102 103 106 109 1012 1015 1018 1021 1024
Name deci centi milli micro nano pico femto atto zepto yocto
Symbol d c m μ n p f a z y
Factor 10−1 10−2 10−3 10−6 10−9 10−12 10−15 10−18 10−21 10−24

However, not all prefixes are commonly used for all units, and that is why they are not included in Calcpad. Full list of the available units is provided in the user manual:


Internal implementation

Any software implementation must allow the units to be compared, converted and scaled. For that purpose, we need to store the information for how units are composed by the base ones. We also need the corresponding scale factors, in order to allow for multiples. Then, we have to define all possible operations like comparison, addition, subtraction, multiplication, division and exponentiation.

To store all this information, we need a special type of data called “structure”. It can contain multiple values of different types in a single variable. In the case of units, we will include a name and two vectors. The first one will store the exponents for all seven physical dimensions (mass, length, time, current, temp, substance, luminosity) and the second one – the respective scaling factors:

Unit = {name: "name", exponents: [p1, p2, p3, p4, p5, p6, p7], factors: [f1, f2, f3, f4, f5, f6, f7]}

A base unit can include only one value of “1” for the respective dimension. All the rest must be exactly zero “0”. Composite units can have several nonzero values for the corresponding dimensions. Bellow are some examples of unit definitions:

Base units:

Kilogram = {name: "kg", exponents: [1, 0, 0, 0, 0, 0, 0], factors: [103, 1, 1, 1, 1, 1, 1]}
Meter = {name: "m", exponents: [0, 1, 0, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}
Second = {name: "s", exponents: [0, 0, 1, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}

Composite units:

Newton = {name: "N", exponents: [1, 1, -2, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}
Pascal = {name: "Pa", exponents: [1, -1, -2, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}


Ton = {name: "t", exponents: [1, 0, 0, 0, 0, 0, 0], factors: [106, 1, 1, 1, 1, 1, 1]}
Millimeter = {name: "mm", exponents: [0, 1, 0, 0, 0, 0, 0], factors: [1, 10-3, 1, 1, 1, 1, 1]}
Minute = {name: "min", exponents: [0, 0, 1, 0, 0, 0, 0], factors: [1, 1, 60, 1, 1, 1, 1]}
Kilonewton = {name: "kN", exponents: [1, 1, -2, 0, 0, 0, 0], factors: [103, 1, 1, 1, 1, 1, 1]}
Megapascal = {name: "MPa", exponents: [1, -1, -2, 0, 0, 0, 0], factors: [106, 1, 1, 1, 1, 1, 1]}

Units consistency

You can compare, add or subtract only values, which units are consistent. For example, “kg + m” does not make any sense, because both operands represent different physical dimensions. The expression “m2 + m3” does not make sense as well, because you are trying to add area to volume. However, “m + mm” is possible, because both represent length and we can convert them to each other. So, units are consistent only if they are of the same dimensions and with the same exponents. What we need to do for our structure, is to check if the exponents vectors match exactly.

Comparison, addition and subtraction

Even if consistent, units are not necessarily equal, because they can have different scaling factors. So, we still cannot operate directly with the respective values. First, we have to convert all values to the same units. For example:

3 dm + 2 cm = 3 dm + 0.2 dm = (3 + 0.2) dm = 3.2 dm

Subtraction and comparison are performed in the same way as the addition. The following algorithm can be used:

  1. Check if the units are consistent (compare the exponent vectors). If not, report an error. Otherwise, continue to step 2.
  2. Compare the scale factors. If different, compute the conversion factor for the second unit and use it to multiply the second value.
  3. Perform the operation and return the result with the units of the first value.

Multiplication, division and exponentiation

Unlike the previous ones, these operations do not require the units to be consistent. Actually, this is the way we use to derive new units by combining the existing ones. In the software, these operations are performed by manipulation of the respective exponents as follows:

  1. Multiplication is performed by addition of the exponents:
    m·m2 = m(1 + 2) = m3
  2. Division is equivalent to exponent subtraction:
    N/m2 = kg·m·s-2/m2 = kg·m(1 - 2)·s-2 = kg·m-1·s-2 = Pa
  3. Exponentiation is performed by multiplication of the old and new exponent:
    (m2)3 = m2·3 = m6

The following algorithm is applied:

  1. Check if there are different scale factors and compute the conversion factor.
  2. Perform the required operation (multiplication, division …) with the units and obtain the resulting units.
  3. Perform the operation with the values, apply the conversion factor and return the result in the calculated units.

Target units

If you do not specify any target units, the result will be decomposed to the base units. For example:

100N·10m = 1000 kg·m2/s2

That is because the program cannot understand the physical meaning of your formulas. Sometimes, the result can be ambiguous and the program will not know what to do. For the above expression:

  • If it is bending moment or torque, the result will be in Nm.
  • If it is work or energy, the result will be in Joules.

In such cases, you have to specify the target units and if they are compatible, the result will be converted automatically. Always write the target units at the end of the expression, separated by a vertical bar “|”:

500N·20m|kJ = 10kJ

2000N·5m|kNm = 10kNm

Quadratic equation – part II

In the previous post we created a simple program to solve a quadratic equation. We used the well known method from school:

"Quadratic equation:
'a*x^2 + b*x + c = 0
a = 3 ', ' b = 4 ', ' c = -5
'Discriminant - ' D = b^2 - 4*a*c
x_1 = (-b - sqr(D))/(2*a)
x_2 = (-b + sqr(D))/(2*a)

In this post we will make our program more professional and add some advanced features. Currently, it is fine for personal use, but if we want to give it to other users, we need to work a little bit harder.

For example, let’s try to enter a = 0 and see what happens. In fact, it will turn into a linear equation, which solution is x = -c/b. However, our program will end up with error due to division by zero. Calcpad will show -∞, +∞ or undefined, if we have 0/0 as it will happen for x_2. It is similar for the case of a negative discriminant D < 0 (unless we use complex numbers).

This is not very good. Every professional program must work correctly for any possible input data. It should either provide a sensible result or a comprehensible message to the user. But how to do this with Calcpad?

The good news is that Calcpad supports boolean expressions and program flow control as follows:

#else if 
#end if

This is a very powerful feature. It allows you to split the solution entirely, depending on some intermediate values. It is not just about using different formulas. You can even display different text and images into the output. It is very difficult to do this with Excel.

Dots “...” mean that you can add as many conditions as you need (do not place the dots into the code) Instead of <condition> you can put any expression that compares values, e.g. a ≡ 0 or D < 0. So, lets add some logic into our program as follows:

"Quadratic equation:
'a*x^2 + b*x + c = 0
a = 3 ', ' b = 4 ', ' c = -5
#if a ≡ 0
    'The coefficient a is equal to zero. The equation is linear:
    x = -c/b
    'Discriminant - ' D = b^2 - 4*a*c
    #if D ≥ 0
        'The discriminant is greater or equal to zero. Real roots exist.
        q = -0.5*(b + sign(b)*sqr(D))
        x_1 = q/a
        x_2 = c/q
        'The discriminant is less than zero. There are no real roots.
    #end if
#end if

Now, the program will display the correct answer in any case. You can copy the above script into Calcpad and try for a = 0 or b = 5. We also made another improvement: The classical formula x1,2 = (-b ± sqr(D))/(2·a) is numerically unstable. The answer will be inaccurate due to the approximate representation of floating point numbers by the computer. The new formula solves this problem.

So far, so good. We managed to improve our program, but it is still not very convenient for other people to use it. When the program gets larger and more complicated, it becomes difficult for users to work directly with the source code and find where to change the input values. There is also a risk to accidentally damage some formulas and produce incorrect results.

That is why, Calcpad provides a quick way to generate input forms directly from the source code. All you need is to put question marks ? wherever you want to enter values. In our program, we need to change the third line as follows:

a = ? ', ' b = ? ', ' c = ?

Then press the “form” button (left to the orange arrow) and you will get the input form:

Input form

But how we made the solution details to disappear during the input? In Calcpad, you can select which part of the output will be visible at each stage by using special keywords:

  • #hide – always hide the content after this place ;
  • #pre   – show the content only in input mode (before calculations);
  • #post – show the content only in output mode (after calculations);
  • #show – always show the remaining content (cancel);

If you want to hide all calculation details during the input, just add #post after the third line. There is also another method for hiding. Since all the output is finally formatted as Html, you can include Html tags and CSS in Calcpad comments. You can use this to enclose some contents in Html comments and it will hide as well:


Do not use this to hide sensitive information because it can be seen in the Html source. For that purpose, we strongly recommend to use #hide. It entirely skips the content in the output Html.

So, our first program is ready. It is pretty much like the one on the official Calcpad website:


The only additional thing is the function graph, but I am going to write an whole new post about graphing. Bellow is the final source code for the quadratic equation program. You can copy it an use it freely:

"Quadratic equation:
'a·x<sup>2</sup> + b·x + c = 0
a = ? ', ' b = ? ', ' c = ?
#if '<!--'a ≡ 0'-->
    'The coefficient a is equal to zero. The equation is linear:
    x = -c/b
    'Discriminant - ' D = b^2 - 4*a*c
    #if '<!--'D ≥ 0'-->
        'The discriminant is greater or equal to zero. Real roots exist.
        q = -0.5*(b + sign(b)*sqr(D))
        x_1 = q/a
        x_2 = c/q
        'The discriminant is less than zero. There are no real roots.
    #end if
#end if

My first program – quadratic equation

Lets write our first program with Calcpad. Most programming languages usually start with “Hello world!”, but for Calcpad, there is nothing much to do, except to write the text in quotes:

"Hello world from Calcpad!"

Then press Enter and the text will appear in the output.

So, let’s do something more useful and solve a real problem. The quadratic equation is simple enough to start with.

All you need to do is to enter the formulas and the text in the “Script” box on the left. Then press Enter or the orange arrow and you will get the results in the “Output” box on the right:

Quadratic equation

Bellow, you can copy the source code and try it by yourself:

"Quadratic equation:
'a*x^2 + b*x + c = 0
a = 3 ', ' b = 4 ', ' c = -5
'Discriminant - ' D = b^2 - 4*a*c
x_1 = (-b - sqr(D))/(2*a)
x_2 = (-b + Sqr(D))/(2*a)

All comments must be enclosed in quotes: double quotes for “titles” and single quotes for ‘simple text’. In this example, "Quadratic equation is a title and 'Discriminant - ' is text. The closing quote can be omitted, if there is nothing further on the line.

Everything outside the quotes is assumed to be math expressions.  Normally, each expression should be on a new line. If they are several expressions on the same line, they must be separated by comments, e.g. 

a = 3 ', ' b = 4 ', ' c = -5

Expressions are defined by using the commonly accepted mathematical notation: numbers, variables, operators “^ / \ % * - + ≡ ≠ ≤ ≥ =” and standard mathematical functions. A short description of the available syntax is initially displayed in the output box. For more information, please check the user manual:


The output is automatically formatted in a professional looking Html report, ready for printing:

  • the sqr function is replaced by the square root symbol;
  • the “^” operator is formatted as superscript;
  • underscores “_” in variable names are replaced by subscripts etc.

All formulas are rendered into the calculation report. Additionally, Calcpad can show variable substitutions, so you can check every value in the output. This is a great advantage over Excel spreadsheets, where the actual formulas are hidden in the cells. It is important for structural calculations to be presented in a way that allows them to be easily followed and verified by the approving authorities.

In the next publication we will add more features to our quadratic equation program.

Getting started

Calcpad is available for both cloud and desktop on the official website http://calcpad.net. You can use it as an online programmable calculator with support for complex numbers, variables, functions and graphing.

online calculator

You can also browse the online library and find professional worksheets for solving equations or calculating areas, volumes and mechanical properties. There is also a rich collection of structural design spreadsheets to Eurocode. You can design reinforced concrete beams, columns or plates, calculate deflections and cracks, punching shear reinforcement or check the detailing requirements for RC elements.

Some worksheets are free to use, but others require subscription. You can create your account and order a subscription plan. Credit card payments are accepted via PayPal.

Calcpad is also available as a desktop application. You can download a 30-day free trial for Windows and create your own calculation programs. The desktop subscription, allows you to download worksheets from the online library and use them on your computer.

The desktop version has basic code editing capabilities and is useful for small to medium sized programs. It supports line numbering, syntax highlighting, automated white space formatting, coping and pasting, etc.

calcpad desktop

However, it is also possible to use another, much more powerful code editor such as Notepad++. It is a free and open source text editing software, with many advanced features. Download and install Notepad++ from the official website https://notepad-plus-plus.org. You can also add the Calcpad language syntax. It is defined in the Notepad++.xml file.


In the next posts, you will find detailed instructions how to set up Calcpad and Notepad++ and write your own programs.

Welcome to Calcpad!

Welcome to my blog!

I am a structural engineer with almost 20 years of experience in the design of nuclear and industrial facilities, factories, residential and public buildings. I am also a fan of engineering, mathematics and programming and you will find a lot of interesting stuff about that here.

For all these years I’ve been dealing mostly with calculations. I have developed a lot of custom spreadsheets and macros to automate my job, using various tools like Excel, VBA, MathCAD, etc. Each of them had a lot of advantages, but I also stumbled upon different kinds of problems.

That is why I created Calcpad. It is a simple but powerful mathematical platform for engineering calculations:

  • It is easy to learn and use. No special programming skills are required, which makes it suitable for all engineers.
  • Calculations are automatically documented into beautiful Html reports that are easy to follow and check.
  • Calcpad spreadsheets can be published online. Web forms are generated automatically out of the source code.

It combines the simplicity of spreadsheets with the power and flexibility of programming in an elegant way. In this blog you will find detailed information about CalcPad, useful tips and tricks, sample code and solutions of variable problems.