> For the complete documentation index, see [llms.txt](https://docs.cosmocloud.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cosmocloud.io/flow-builder/node-types/variable-nodes/mathematical/complex-math-expr.md).

# Complex Math Expr

**Complex Math Expr** node is designed to evaluate complex mathematical expressions and store the result in a specified variable. This node allows for more sophisticated calculations than simple arithmetic operations.

### Properties Panel

| **Field**     | **Description**                                                        | **Required** |
| ------------- | ---------------------------------------------------------------------- | ------------ |
| Node name     | [Node Name](/flow-builder/node-name.md)                                | true         |
| Variable Name | The name of the variable where the result item will be stored          | true         |
| Value         | A JSON object representing the mathematical expression to be evaluated | true         |

### Usage

1. Specify a variable name where the result will be stored.
2. Define the expression using a JSON object with supported operators and functions.

### Expression Format

The expression is defined as a JSON object where each key is an operator or function, and its value is an array of operands or arguments. The node supports a wide range of operations as defined in the [CosmoCloud Query Language (CQL)](/flow-builder/cql-cosmocloud-query-language.md) documentation.

Example:

```json
{
  "$multiply": [
    {
      "$add": ["$.variables.base_price", "$.variables.tax_amount"]
    },
    {
      "$subtract": [1, "$.variables.discount_rate"]
    }
  ]
}
```

### Returns

The node stores the result of the expression evaluation in the specified variable. You can access this using Magical Autocomplete (e.g., `$.variables.<variable_name>`) in any node below this node.

### Example

Let's say you want to calculate a complex price adjustment with conditions:

1. Set Variable name to `adjusted_price`
2. Set Expression to:

   ```json
   {
     "$multiply": [
       "$.variables.base_price",
       {
         "$cond": [
           {
             "$.variables.loyalty_points": {
               "$gt": 1000
             }
           },
           0.9,
           1
         ]
       }
     ]
   }
   ```

   \
   This will apply a 10% discount if the customer has more than 1000 loyalty points, and store the result in the `adjusted_price` variable.

### Best Practices

* Refer to the CQL documentation for the full list of available operators and functions.
* Be mindful of the data types you're working with, especially when using type-specific operators or functions.
* Also note that the expression can have only one operator at one level. For example:

  ```json
  {
    "$multiply": [...], -> only one
    "$add": [...],  -> this is not allowed
  }
  ```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.cosmocloud.io/flow-builder/node-types/variable-nodes/mathematical/complex-math-expr.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
