# 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](https://docs.cosmocloud.io/flow-builder/node-name)         | 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)](https://docs.cosmocloud.io/flow-builder/cql-cosmocloud-query-language) 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
  }
  ```
