# Logical & Comparison functions

CalcTree supports standard logical and comparison functions using [MathJS syntax](https://mathjs.org/docs/reference/functions.html#logical) and [comparison operators](https://mathjs.org/docs/reference/functions.html#relational).

These functions return `true` or `false`, and are often used in conditional expressions and ternary operations (e.g. `a > b ? "yes" : "no"`).

### If Statements

MathJS does not use a traditional `if(...) {}` structure. Instead, native CalcTree parameters support **conditional logic** using the **ternary operator**:

```
condition ? value_if_true : value_if_false
```

#### Example: Basic if logic

```
result = load > limit ? "Fail" : "Pass"
```

#### Nested conditions (like else if)

```
check = ratio < 1 ? "OK" : ratio < 1.5 ? "Warning" : "Fail"
```

### Comparison Functions

<table><thead><tr><th>Function</th><th width="219">Description</th><th>CalcTree Example</th></tr></thead><tbody><tr><td><code>IF(condition, a, b)</code></td><td>Returns <code>a</code> if true, <code>b</code> if false</td><td><code>IF(load > 100, "Fail", "Pass")</code> → <code>"Pass"</code></td></tr><tr><td><code>equal(a, b)</code></td><td>Checks strict equality</td><td><code>equal(2, 2)</code> → <code>true</code></td></tr><tr><td><code>unequal(a, b)</code></td><td>Checks inequality</td><td><code>unequal(2, 3)</code> → <code>true</code></td></tr><tr><td><code>smaller(a, b)</code></td><td>Less than</td><td><code>smaller(1, 2)</code> → <code>true</code></td></tr><tr><td><code>smallerEq(a, b)</code></td><td>Less than or equal</td><td><code>smallerEq(2, 2)</code> → <code>true</code></td></tr><tr><td><code>larger(a, b)</code></td><td>Greater than</td><td><code>larger(3, 1)</code> → <code>true</code></td></tr><tr><td><code>largerEq(a, b)</code></td><td>Greater than or equal</td><td><code>largerEq(3, 3)</code> → <code>true</code></td></tr></tbody></table>

### Natural and String Comparison

| Function               | Description                              | CalcTree Example                   |
| ---------------------- | ---------------------------------------- | ---------------------------------- |
| `compare(a, b)`        | General numerical comparison             | `compare(2, 3)` → `-1`             |
| `compareText(a, b)`    | Lexical string comparison                | `compareText("a", "b")` → `-1`     |
| `compareNatural(a, b)` | Natural-order comparison (e.g., v1 < v2) | `compareNatural("2", "10")` → `-1` |

### Deep & Elementwise Equality

| Function          | Description                               | CalcTree Example                     |
| ----------------- | ----------------------------------------- | ------------------------------------ |
| `deepEqual(a, b)` | Element-wise equality for arrays/matrices | `deepEqual([1, 2], [1, 2])` → `true` |
| `equalText(a, b)` | Text string equality                      | `equalText("abc", "abc")` → `true`   |

### Logical Functions

| Function    | Description  | CalcTree Example            |
| ----------- | ------------ | --------------------------- |
| `and(a, b)` | Logical AND  | `and(true, true)` → `true`  |
| `or(a, b)`  | Logical OR   | `or(false, true)` → `true`  |
| `not(a)`    | Logical NOT  | `not(true)` → `false`       |
| `xor(a, b)` | Exclusive OR | `xor(true, false)` → `true` |

### Notes for CalcTree

* Logical outputs (`true`, `false`) can be used in conditional formulas like:

  ```lua
  result = load > limit ? "Fail" : "Pass"
  ```
* For multi-value comparisons (e.g. arrays), use `deepEqual()` instead of `equal()`.
* Comparisons between unit-bearing quantities must be dimensionally consistent (e.g. `2 m > 1 m` is valid, `2 m > 1 kg` is not).

{% hint style="info" %}
📘 **Looking for more functions?**\
CalcTree’s expression engine is powered by [MathJS](https://mathjs.org/docs/reference/functions.html).\
For a full list of available functions, visit the [MathJS Function Reference](https://mathjs.org/docs/reference/functions.html).\
Most functions listed there are supported in CalcTree unless otherwise noted.
{% endhint %}


---

# 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://calctree.gitbook.io/docs/calculations/creating-calculations/maths-equations/native-functions/logical-and-comparison-functions.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.
