Creating an Online Calculator (2): Algorithms and Code

By James Harry Morris

Previously I uploaded a post about an online calculator that I had created for converting the shakkanhō 尺貫法 units of measurement into their metric and imperial counterparts. Following the publication of the aforementioned post, I realized that it may be of interest to readers and those working on similar projects if I shared and explained the code for the equations used in the calculator.

As noted in the initial post, the calculator was written in Apache Groovy on an online calculator creation system known as vCalc with much of the code based on the system’s Biblical Lengths equation. I have since edited the code, annotated it, and made it accessible on GitHub, however, because vCalc automatically fills in the context and other aspects of the code in order to create its calculators, their appearance, and features such as drop-down menus, the code that I offer is little more than the basic algorithms needed to create the calculator’s equations.

The Code

The algorithms available on GitHub are offered in five separate files each corresponding to a different type of measurement; area, length, volume, weight/mass, and rice weights. The code in each of these files does three primary things:

  1. Defines a series of units (variables).
  2. Creates a switch in order to choose between these variables.
  3. Provides an equation and defines how that equation will be answered.

In the source code written on vCalc, I defined the units using the “getConstant” function. This imported the definitions of the units from elsewhere on vCalc’s database according to a numerical ID.

Screen Shot 2018-09-21 at 21.01.58

An example of the “getConstant” function used in the code on vCalc.

Since people not using vCalc lack access to this database, I decided to replace the “getConstant” function for the GitHub version of the code. As such, I defined each unit according to a numerical metric equivalent. This effectively combined the very simple code that I had written to define the shakkanhō units of measurement on vCalc with the equations I had written to import these into the calculator.

Screen Shot 2018-09-21 at 21.03.30

Examples of the definitions supplied in the GitHub version of the code.

The “switch” function included in the code (and based on the code for the aforementioned Biblical Lengths equation) allows a single type of unit to be selected for use in the algorithm and is suitable for creating features such as a drop-down menu. In such a scenario the function tells the computer to define the variable “y” as the selected shakkanhō unit.

Screen Shot 2018-09-21 at 21.10.45

The switch function.

Finally, the code defines and returns the variable “x” (y multiplied by the inputted numerical value). The vCalc system allows this answer to be automatically converted into other units of measurement, however, the GitHub version of the code will only return the answer in the metric unit in which the variables have been defined (in the case of the example used here cm² or m²).

Screen Shot 2018-09-21 at 21.15.13

Defining and returning “x”.

Further Notes

The code works well within the vCalc system, which as noted adds the additional code needed to run the equations. For those wishing to use the source code in their own projects, it will be necessary to write additional code in order to run it. Nevertheless, the algorithms are potentially useful for those working on similar projects, and I, therefore, believed that it was important to edit and share them. Over the coming months, I intend to add to the code in order to create a downloadable conversion calculator application. I will post again on this topic if I am successful.

Links

GitHub repository containing the source code/algorithms

The previous article on the calculator

 

One comment

  1. Pingback: Creating an Online Calculator for the Conversion of Shakkanhō (Japanese) Units | The Digital Orientalist

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: