> For the complete documentation index, see [llms.txt](https://docs.k3-labs.com/introduction/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.k3-labs.com/introduction/automate/functions-overview/transform-function-details/transform-custom-formula.md).

# Transform Custom Formula

### Overview

The **Transform Function** in K3 Labs allows users to manipulate and enrich data within workflows. The **Custom Formula** feature enables users to apply complex mathematical expressions and logical operations on input data, making it a powerful tool for data transformation.

### Supported Expressions

The **Custom Formula** function supports a variety of mathematical operations on floating-point numbers:

#### Binary Operators

* `+` (addition)
* `-` (subtraction)
* `*` (multiplication)
* `/` (division)
* `%` (remainder/modulus)
* `^` (power/exponentiation)

#### Unary Operators

* `+` (positive sign)
* `-` (negation)

#### Built-in Functions

The **Custom Formula** function provides built-in mathematical functions, similar to those found in the Rust standard library:

* **Basic Functions:**
  * `sqrt(x)`: Square root
  * `abs(x)`: Absolute value
  * `exp(x)`: Exponential function (e^x)
  * `ln(x)`: Natural logarithm
* **Trigonometric Functions:**
  * `sin(x)`, `cos(x)`, `tan(x)`: Sine, cosine, and tangent
  * `asin(x)`, `acos(x)`, `atan(x)`, `atan2(y, x)`: Inverse trigonometric functions
  * `sinh(x)`, `cosh(x)`, `tanh(x)`: Hyperbolic sine, cosine, and tangent
  * `asinh(x)`, `acosh(x)`, `atanh(x)`: Inverse hyperbolic functions
* **Rounding Functions:**
  * `floor(x)`: Rounds down to the nearest integer
  * `ceil(x)`: Rounds up to the nearest integer
  * `round(x)`: Rounds to the nearest integer
  * `roundto(x, d)`: Rounds `x` to `d` decimal places. Example: `roundto(10.55235, 2)` returns `10.55`
* **Sign Function:**
  * `signum(x)`: Returns -1, 0, or 1 depending on the sign of `x`&#x20;
* **Time Function:**
  * `Now`: Returns the current timestamp in **UTC** as **seconds** since the Unix epoch. No parentheses are required (e.g., `Now`).

#### Other Functions

* `max(x, ...)`: Returns the maximum value from one or more numbers
* `min(x, ...)`: Returns the minimum value from one or more numbers

#### Constants

* `pi`: Mathematical constant π (3.14159...)
* `e`: Euler's number (2.718...)

### Usage

The **Custom Formula** function can be used within a Transform Function node in the K3 Labs workflow builder. Users can define custom variables and apply formulas directly to input data.

#### Example 1: Basic Arithmetic

**Expression:**

```plaintext
(x + y) * 2
```

**Usage:**

* If `x = 5` and `y = 3`, the result will be `16`.

#### Example 2: Using Built-in Functions

**Expression:**

```plaintext
sqrt(a^2 + b^2)
```

**Usage:**

* If `a = 3` and `b = 4`, the result will be `5` (Pythagorean theorem).

#### Example 3: Conditional Calculation

**Expression:**

```plaintext
if(x > 0, ln(x), 0)
```

**Usage:**

* If `x = 10`, the result will be `ln(10) ≈ 2.302`.
* If `x = -5`, the result will be `0` (avoiding logarithm of a negative number).

#### Example 4: Finding Maximum Value

**Expression:**

```plaintext
max(a, b, c)
```

**Usage:**

* If `a = 10`, `b = 15`, and `c = 7`, the result will be `15`.

### Integration with K3 Labs Workflows

The **Custom Formula** function can be used in:

* **Data Filtering**: Modify or filter data before passing it to the next step.
* **Alert Triggers**: Calculate thresholds and trigger alerts based on computed values.
* **Trading Strategies**: Apply mathematical operations on financial data for automated trading.
* **API Data Processing**: Transform API responses before sending them to another service.
