# Mathematical Expressions

The EasyEdge Operations Engine includes a comprehensive mathematical expression parser that supports a wide range of operators and functions. This is the foundation for performing any numerical calculation within your Computation String. All calculations are performed using high-precision 64-bit floating-point numbers.

***

## Core Concepts

### Operands

Operands are the variables of your expressions. They are represented by **single lowercase letters from a to z**. Each operand must be linked to a system object (like a sensor tag, a data field, or another operation's result) in the operation's configuration.

At runtime, just before the expression is evaluated, the engine replaces each operand with the current numerical value of the object it is linked to. This real-time substitution is what makes the expressions dynamic and responsive to the state of your system.

{% hint style="info" %}
🧠 **Example**: If operand a is linked to a temperature sensor that currently reads 25.5, the expression a \* 1.8 + 32 will be evaluated as 25.5 \* 1.8 + 32.
{% endhint %}

### Literals

Literals are fixed, constant values written directly into the expression. The engine supports standard numeric formats:

* **Integers**: 100, -5, 0, 0x2F
* **Floating-point numbers**: 98.6, -0.01, 3.14159

***

## Operators

Operators are the symbols that perform actions on operands and literals. The engine respects the standard mathematical order of operations.

### Order of Operations (Precedence)

Expressions are not evaluated strictly from left to right. Operators have a defined precedence, which determines the order of execution. You can always use parentheses () to override this natural order and force a specific part of an expression to be evaluated first.

The order of precedence, from highest to lowest, is:

1. **Parentheses**: ()
2. **Power**: ^
3. **Unary Minus**: - (e.g., -a)
4. **Multiplication, Division, Modulo**: \*, /, %
5. **Addition, Subtraction**: +, -
6. **Relational Operators**: ==, !=, <, <=, >, >=
7. **Logical & Bitwise Operators**: not, and, or, xor, nand, nor, xnor, mand, mor

{% hint style="info" %}
🧠 **Example**: 3 + 4 \* 2 evaluates to 11 because multiplication has higher precedence. (3 + 4) \* 2 evaluates to 14 because the parentheses force the addition to happen first.
{% endhint %}

### Arithmetic Operators

These are the fundamental operators for performing calculations.

| **Operator** | **Description**        | **Example** |
| ------------ | ---------------------- | ----------- |
| +            | Addition               | a + 10      |
| -            | Subtraction            | b - 5.5     |
| \*           | Multiplication         | c \* 2      |
| /            | Division               | d / 3       |
| ^            | Power (Exponentiation) | a ^ 2       |
| %            | Modulo (Remainder)     | b % 3       |

### Relational Operators

These operators compare two values and return 1.0 (for true) or 0.0 (for false). They are essential for creating conditional logic, especially within the if() function.

| **Operator** | **Description**          | **Example** |
| ------------ | ------------------------ | ----------- |
| == or =      | Equal to                 | a == 100    |
| !=           | Not equal to             | b != 0      |
| <            | Less than                | c < 50      |
| <=           | Less than or equal to    | d <= 10.5   |
| >            | Greater than             | a > 0       |
| >=           | Greater than or equal to | b >= 99     |

### Logical and Bitwise Operators

These operators combine multiple relational expressions or perform bit-level manipulations.

| **Operator** | **Description**                                               | **Example**      |
| ------------ | ------------------------------------------------------------- | ---------------- |
| and or &     | Logical AND. Returns 1.0 if both inputs are non-zero.         | a > 0 and b < 10 |
| or or \|     | Logical OR. Returns 1.0 if either input is non-zero.          | a == 1 or a == 2 |
| not          | Logical NOT. Returns 1.0 if the input is zero, 0.0 otherwise. | not (b == 0)     |
| xor          | Logical XOR. Returns 1.0 if inputs are different.             | a xor b          |
| nand         | Logical NAND. The inverse of and.                             | a nand b         |
| nor          | Logical NOR. The inverse of or.                               | a nor b          |
| xnor         | Logical XNOR. The inverse of xor.                             | a xnor b         |
| mand         | Multi-argument AND. Returns 1.0 only if all inputs are true.  | a mand b mand c  |
| mor          | Multi-argument OR. Returns 1.0 if any input is true.          | a mor b mor c    |

***

## Built-in Mathematical Functions

A rich library of mathematical functions is available for more advanced calculations.

### General Functions

| **Function**       | **Description**                                                                  | **Example**                |
| ------------------ | -------------------------------------------------------------------------------- | -------------------------- |
| abs(x)             | Absolute value of x                                                              | abs(a - 100)               |
| avg(x1,…,xn)       | Average of all inputs                                                            | avg(a, b, c)               |
| ceil(x)            | Rounds x up to the nearest integer                                               | ceil(a / 10.5)             |
| clamp(min,x,max)   | Clamps x to be within the range \[min, max]                                      | clamp(0, a, 100)           |
| equal(x,y)         | Numeric test for equality with a small tolerance                                 | equal(a, 100.001)          |
| floor(x)           | Rounds x down to the nearest integer                                             | floor(b \* 3.14)           |
| frac(x)            | Fractional part of x                                                             | frac(3.7) returns 0.7      |
| hypot(x,y)         | Calculates √(x²+y²)                                                              | hypot(a, b)                |
| iclamp(min,x,max)  | Inverse clamp. If x is in \[min,max], returns x, else returns the nearest bound. | iclamp(-1, 2, 1) returns 1 |
| inrange(min,x,max) | Returns 1.0 if x is in the range \[min, max], otherwise 0.0.                     | inrange(0, a, 100)         |
| max(x1,…,xn)       | Maximum value among the inputs                                                   | max(c, d, a)               |
| min(x1,…,xn)       | Minimum value among the inputs                                                   | min(a, b, 10)              |
| mul(x1,…,xn)       | Product of all inputs                                                            | mul(a, b, c)               |
| not\_equal(x,y)    | Numeric test for inequality with a small tolerance                               | not\_equal(a, 99.999)      |
| pow(x,y)           | x raised to the power of y (same as x ^ y)                                       | pow(a, 3)                  |
| root(x,n)          | The n-th root of x                                                               | root(27, 3) returns 3      |
| round(x)           | Rounds x to the nearest integer                                                  | round(c)                   |
| roundn(x,n)        | Rounds x to n decimal places                                                     | roundn(a, 3)               |
| sgn(x)             | Sign of x (-1, 0, or 1)                                                          | sgn(a - 50)                |
| sqrt(x)            | Square root of x                                                                 | sqrt(a)                    |
| sum(x1,…,xn)       | Sum of all inputs                                                                | sum(a, b, c)               |
| trunc(x)           | Integer part of x (truncates toward zero)                                        | trunc(-3.9) returns -3     |

### Conditional Function

| **Function** | **Description**                                             | **Example**      |
| ------------ | ----------------------------------------------------------- | ---------------- |
| if(cond,t,f) | If cond is non-zero (true), returns t, otherwise returns f. | if(a > 10, b, c) |

### Exponential & Logarithmic Functions

| **Function** | **Description**                                       | **Example** |
| ------------ | ----------------------------------------------------- | ----------- |
| exp(x)       | e raised to the power of x                            | exp(a)      |
| expm1(x)     | e^x - 1, more accurate for small x                    | expm1(a)    |
| log(x)       | Natural logarithm of x                                | log(a)      |
| log1p(x)     | Natural logarithm of (1+x), more accurate for small x | log1p(a)    |
| log2(x)      | Base-2 logarithm of x                                 | log2(a)     |
| log10(x)     | Base-10 logarithm of x                                | log10(b)    |
| logn(x,n)    | Logarithm of x to base n                              | logn(a, 16) |

### Trigonometric Functions

**Note**: All angle inputs and outputs are in radians.

| **Function** | **Description**                                       | **Example** |
| ------------ | ----------------------------------------------------- | ----------- |
| sin(x)       | Sine of x                                             | sin(b)      |
| cos(x)       | Cosine of x                                           | cos(c)      |
| tan(x)       | Tangent of x                                          | tan(d)      |
| asin(x)      | Arc sine of x                                         | asin(a)     |
| acos(x)      | Arc cosine of x                                       | acos(b)     |
| atan(x)      | Arc tangent of x                                      | atan(c)     |
| atan2(y,x)   | Arc tangent of y/x, using signs to determine quadrant | atan2(a, b) |
| sinh(x)      | Hyperbolic sine of x                                  | sinh(a)     |
| cosh(x)      | Hyperbolic cosine of x                                | cosh(b)     |
| tanh(x)      | Hyperbolic tangent of x                               | tanh(c)     |
| asinh(x)     | Inverse hyperbolic sine of x                          | asinh(a)    |
| acosh(x)     | Inverse hyperbolic cosine of x                        | acosh(b)    |
| atanh(x)     | Inverse hyperbolic tangent of x                       | atanh(c)    |
| cot(x)       | Cotangent of x                                        | cot(a)      |
| csc(x)       | Cosecant of x                                         | csc(b)      |
| sec(x)       | Secant of x                                           | sec(c)      |
| sinc(x)      | Sinc function (sin(x)/x)                              | sinc(a)     |

### Angle Conversion Functions

| **Function** | **Description**              | **Example**   |
| ------------ | ---------------------------- | ------------- |
| deg2rad(d)   | Converts degrees to radians  | deg2rad(180)  |
| deg2grad(d)  | Converts degrees to gradians | deg2grad(90)  |
| rad2deg(r)   | Converts radians to degrees  | rad2deg(pi)   |
| grad2deg(g)  | Converts gradians to degrees | grad2deg(100) |

### Special Functions

| **Function** | **Description**                         | **Example** |
| ------------ | --------------------------------------- | ----------- |
| erf(x)       | Error function                          | erf(a)      |
| erfc(x)      | Complementary error function            | erfc(a)     |
| ncdf(x)      | Normal cumulative distribution function | ncdf(a)     |

***

## Bitwise Functions (b\* prefix)

For low-level data manipulation, a full suite of bitwise functions is available. These functions operate on the **53-bit integer representation** of the values (any fractional part is truncated). All bitwise functions are prefixed with b.

| **Function** | **Description**              | **Example**   |
| ------------ | ---------------------------- | ------------- |
| band(x,y)    | Bitwise AND (x & y)          | band(a, 15)   |
| bor(x,y)     | Bitwise OR (x \| y)          | bor(a, 0x01)  |
| bxor(x,y)    | Bitwise XOR (x ^ y)          | bxor(a, 0xff) |
| bnot(x)      | Bitwise NOT (\~x)            | bnot(a)       |
| bshl(x,n)    | Bitwise Shift Left (x << n)  | bshl(a, 2)    |
| bshr(x,n)    | Bitwise Shift Right (x >> n) | bshr(a, 1)    |
| bnand(x,y)   | Bitwise NAND (\~(x & y))     | bnand(a, b)   |
| bnor(x,y)    | Bitwise NOR (\~(x \| y))     | bnor(a, b)    |
| bxnor(x,y)   | Bitwise XNOR (\~(x ^ y))     | bxnor(a, b)   |

#### Understanding Bitwise Operations: A Practical Example

Imagine a device status register is read into operand a. The value of a is 13. You want to check if the 2nd bit (value 2) and the 4th bit (value 8) are both active.

* 13 in binary is 00001101
* The mask to check for both bits is 2 | 8, which is 10.
* 10 in binary is 00001010

**Computation String**: band(a, 10) == 10

**Evaluation Steps**:

1. band(13, 10) is calculated.

* 00001101 (13)
* & 00001010 (10)
* \----------
* 00001000 (8)

2. The result 8 is then compared: 8 == 10. This is false, so the expression returns 0.0. This correctly indicates that only one of the requested bits (the 4th bit) was active, not both.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://easyedge.gitbook.io/easyedge/reference/expressions-operations-engine/mathematical-expressions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
