CL Newsletter, August 2021 - Computations in CL

You may be aware that the primary function of Computation Layer is to connect components, and that performing actual computations in CL can be challenging, at least in comparison to some other computer languages. This month, we thought it would be useful to dedicate some time to calculations in CL, highlighting the advantages and disadvantages of the way calculations are generally handled as well as the advantages and disadvantages of each individual method.

numericValue

The first way to perform simple calculations is to utilize a function built for something else. The numericValue function is made to calculate the value of any string. In other words:

numericValue(3) evaluates to 3

numericValue(3/4) evaluates to 0.75

numericValue(2+3) evaluates to 5

By combining it with some dynamic text, you can perform calculations based on a variety of inputs. For example:

Input:

valueA = 12

valueB = 7

numericValue(`${valueA}+${valueB}`)

Output:

19

You can also perform simple calculations using a student’s input:

numericValue(${input.numericValue}*2) outputs double the value of the input.

Given the fact that calculations aren’t the intended use of numericValue, this method comes with some pretty significant limitations. Primarily, numericValue computing the value of a string means that any numeric input is turned into a string and then back into a number. That double conversion can lead to errors. Try this activity to see what we mean.

Try It!

Because of this we recommend only using numericValue for fixed calculations (e.g., numericValue(\sqrt{5}) ), or if you are certain that you can account for all possible variables.

simpleFunction

The recommended way to perform calculations in CL is to create a function that performs the desired operations, then evaluate it at the values you want to use. Here’s how:

Step 1: Create a function.

  • Use simpleFunction to create the expression you’d like to use for calculating. For example, simpleFunction(x+3) takes the input and adds 3 to it.
  • If you want to use multiple variables, list them out after your expression separated by commas:

simpleFunction(x+y,x,y) calculates the sum of x and y.

Step 2: Evaluate your function at the desired values.

  • Once your function is created, evaluate it at the values you want using evaluateAt.

simpleFunction(x+y,x,y).evaluateAt(5,3) evaluates to 8.

simpleFunction(x+3).evaluateAt(input.numericValue) adds 3 to whatever value is entered into a math input.

In addition to evaluating numeric values without first converting to strings, simpleFunction is especially useful when you want to perform the same operation over and over again. As stated previously, simpleFunction is the recommended method for performing variable computations in CL. This is because of its reliability and durability. Give these features a try with these two screens!

Try These Two Screens

Advantages and Disadvantages

You may be wondering, “Why the extra steps?” And we totally understand. Performing simple computations like testNumber = 3+4 is so much simpler than:

testNumber = numericValue(3+4)

or

testNumber = simpleFunction(3+4).evaluateAt(0)

But what if you want to do more than simple operations? The primary advantage of doing things this way is that we can bring in almost any function from the calculator and perform the evaluations in CL.

Create your function in the calculator. Test it with a few values.

Copy the expression and paste it into a simpleFunction.

Done. Try it!

This method should work for almost any expression that you can make in the calculator, provided that the expression doesn’t use any list operations.