Formula Field

Value variables

If you've ever used Google Spreadsheet or MS Excel, you should be familiar with variables in the formula. In Calculoid formula, there aren't used rows and columns for identification of a value (because there aren't any), but each field has its unique identifier (for example F123).

You will use these field IDs instead of actual values. For example, I want to create this formula:

`(A + B) * 45 / C`

So I create field A, B and C. Doesn't matter what type of the field it is. Each field has its unique ID. Let's say field A has ID F123, field B has ID F124 and field C has ID F135. Final formula will be like this:

`(F123 + F124) * 45 / F135`

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

Calculoid uses http://silentmatt.com/javascript-expression-evaluator/ for formula evaluation.

Clone this wiki locally
You can’t perform that action at this time.
Press h to open a hovercard with more details.