Content

Formula field - all about Syntax and Operators

About to create your first Calculoid formula field? Have a look at this post to get acquainted with Calculoid syntax and operators. 

Expression Syntax

The parser accepts a pretty basic grammar. Operators have the normal precidence — f(x,y,z) (function calls), ^ (exponentiation), *, /, and % (multiplication, division, and remainder), and finally +, -, and || (addition, subtraction, and string concatenation) — and bind from left to right (yes, even exponentiation… it’s simpler that way).

There’s also a “,” (comma) operator that concatenates values into an array. It’s mostly useful for passing arguments to functions, since it doesn't always behave like you would think with regards to multi-dimensional arrays. If the left value is an array, it pushes the right value onto the end of the array, otherwise, it creates a new array “[left, right]“. This makes it impossible to create an array with another array as it’s first element.

Inside the first argument of the if function can be used these operators to compare expressions:

  • == Equal
  • != Not equal
  • > Greater than
  • >= Greater or equal than
  • < Less than
  • <= Less or equal than
  • and Logical AND operator
  • or Logical OR operator

Example of if function: if(1 and 2 <= 4, 2, 0) + 2 = 4

Function operators

The parser has several built-in “functions” that are actually operators. The only difference from an outside point of view, is that they cannot be called with multiple arguments and they are evaluated by the simplify method if their arguments are constant.

  • sin(x) Sine of x (x is in radians)
  • cos(x) Cosine of x (x is in radians)
  • tan(x) Tangent of x (x is… well, you know)
  • asin(x) Arc sine of x (in radians)
  • acos(x) Arc cosine of x (in radians)
  • atan(x) Arc tangent of x (in radians)
  • sqrt(x) Square root of x. Result is NaN (Not a Number) if x is negative.
  • log(x) Natural logarithm of x (not base-10). It’s log instead of ln because that’s what JavaScript calls it.
  • abs(x) Absolute value (magnatude) of x
  • ceil(x) Ceiling of x — the smallest integer that’s >= x.
  • floor(x) Floor of x — the largest integer that’s <= x
  • round(x, n)) X, rounded to the nearest integer or to n decimal places, using “gradeschool rounding”.
  • exp(x) ex (exponential/antilogarithm function with base e)
  • random(n) Get a random number in the range [0, n). If n is zero, or not provided, it defaults to 1.
  • fac(n) n! (factorial of n: “n * (n-1) * (n-2) * … * 2 * 1″)
  • min(a,b,…) Get the smallest (“minimum”) number in the list
  • max(a,b,…) Get the largest (“maximum”) number in the list
  • pyt(a, b) Pythagorean function, i.e. the c in “c2 = a2 + b2“
  • pow(x, y) xy. This is exactly the same as “x^y”. It’s just provided since it’s in the Math object from JavaScript
  • atan2(y, x) arc tangent of x/y. i.e. the angle between (0, 0) and (x, y) in radians.
  • if(c, a, b) The condition function where c is condition, a is result if c is true, b is result if c is false
  • len(n) The character length of n. i.e. len(123) = 3
  • todayDate () Return today's date in the standard format.
  • newDate (year, month, days) Return date in the standard format based on parameters year, month, days.
  • dateFormat (date, format) Return date in a custom format or in absolute date format (in milliseconds).Different formats can be found: https://docs.angularjs.org/api/ng/filter/date.
  • getTime (date) Returns date in absolute date format (in milliseconds).
  • dateAdd (date, type, value) Adds value to the date. Types: https://docs.angularjs.org/api/ng/filter/date. Returns result in absolute date format. Can be used with dateFormat (dateAdd (date,type,value), 'dd.MM.yyyy').
  • dateDeduct (date, type, value) Takes value to the date. Types: https://docs.angularjs.org/api/ng/filter/date. Returns result in absolute date format. Can be used with dateFormat (dateAdd (date,type,value), 'dd.MM.yyyy').
Additional information

Start Free Now

Create your own web calculator in a matter of minutes.
Test all features in the 30-day free trial. 

Sign Up Free

No credit card or installation needed