All the Math Functions!

12/09/2015 Posted by Brandon

When we first launched our equation editor we were pretty excited: the ability to use field values in mathematical (and date-driven) equations opened up exciting functionality.

We considered adding more advanced equations like rounding or exponents, but decided to keep it easy with basic arithmetic. We figured our users would appreciate the simplicity and how well it worked.

Boy were we wrong.

We immediately started seeing demands for all sorts of functions (is a hyperbolic tangent function really needed? yes, yes it is).

Many of these opened up major features like mortgage amortization and equipment depreciation. It quickly became a no-brainer decision: we had to completely redesign our equation editor.

We recently released this upgraded equation editor, complete with a whole new library of functions:

A major change is simply how it works. Instead of a largely point and click approach we’ve opted for a typing-based approach:

  • As you type a dropdown menu of fields and functions becomes available to autocomplete.
  • Each field or function in the menu provides some additional contextual info about how to use it.
  • As you construct your equation you’ll get instant feedback on how it works and whether it’s valid above the editor.

New functions

We included a whole slew of mathematical functions that are now available to integrate with your equations.

Some highlights include:

  • ceil – Round a value up to the nearest integer. Example: ceil(4.2) = 5
  • factorial – Compute the factorial of a number. Example: factorial(5) = 120
  • max – Get the maximum of a list of values. Example: max(1,6,9) = 9
  • mean – Compute the mean of a list of values. Example: mean(1,6,8) = 5
  • median – Compute the median of a list of values. Example: median(8,9,1) = 8
  • min – Get the minimum of a list of values. Example: min(42, 100, 3) = 3
  • pow – Calculate the base to the exponent power. Example: pow(7,2) = 49
  • randomInt – Get a random integer between two values. Example: random(1,42) = 36
  • std – Compute the standard deviation of a list of values. Example: std(1,5,42,78) = 36.07861785970558
  • sqrt – Calculate the square root of a number. Example: sqrt(81) = 9
You can view the full list of functions here.

Conditional Equations

You could always use conditional rules to include logic with field values an equation uses, or to choose from multiple equations.

You can now include logic right in the equation to create conditional equations.

It’s called a ternary operator and works like this: (condition ? equation_1 : equation_2)

This means if your condition is true, Knack will use the equation followed by the ? symbol. If your condition is false, Knack will use the second equation followed by the : symbol.