> For the complete documentation index, see [llms.txt](https://help.sdocs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.sdocs.com/sdocs/advanced-template-logic/perform-calculations/math-tags-null-value-error-ending-position-out-of-bounds-22-stack-invalid-double.md). Agents can also search and read this documentation through its [MCP server](https://help.sdocs.com/~gitbook/mcp).

# Math Tags Null Value Error: Ending Position Out of Bounds (22) Stack Invalid Double

This error usually means a field inside a `<MATH>` tag is blank.

S-Docs expects a number.

If a merge field returns `null` instead, the calculation can fail with errors like:

* `Ending position out of bounds: 22 STACK`
* `Invalid double`

### Fix 1: Replace blank values with `0`

This is the fastest fix.

Add `replaceall=" ,0"` to any numeric field that might be blank.

{% code title="null-safe-math.xml" %}

```xml
<MATH>
  {{!Opportunity.Amount replaceall=" ,0"}} +
  {{!Opportunity.CustomField__c replaceall=" ,0"}}
</MATH>
```

{% endcode %}

Use this pattern when:

* the field is numeric
* blank values should behave like zero
* you want the calculation to always run

### Fix 2: Only run math when data exists

Use a named query and conditional logic when a missing value means the calculation should not run at all.

This pattern first checks whether matching records exist.

Then it runs the calculation only when there is data to use.

{% code title="conditional-math-with-named-queries.xml" %}

```xml
<!--{{!<LineItemsSOQL>
<class>none</class>
<queryname>myQuery1</queryname>
<soql>
SELECT count(ID) cid
FROM Opportunity
WHERE AccountId = '{{!Account.ID}}'
</soql>
</LineItemsSOQL>}}-->

<!--{{!<LineItemsSOQL>
<class>none</class>
<queryname>myQuery2</queryname>
<soql>
SELECT SUM(Amount) total, SUM(CustomField__c) reduce
FROM Opportunity
WHERE AccountId = '{{!Account.ID}}'
</soql>
</LineItemsSOQL>}}-->

<!--RENDER='{{!myQuery1.cid}} > 0' -->
<MATH>{{!myQuery2.total}} + {{!myQuery2.reduce}}</MATH>
<!--ENDRENDER-->
```

{% endcode %}

Use this pattern when:

* the calculation depends on query results
* zero would be misleading
* you only want output when records exist

{% hint style="info" %}
This issue is commonly tied to the `Invalid double` error. If you see both messages, check for blank numeric fields first.
{% endhint %}

### Quick checklist

Before you test again, confirm these items:

* every field inside `<MATH>` returns a number
* blank numeric fields use `replaceall=" ,0"` when needed
* query-based math is wrapped in `RENDER` when results may be missing
* your `<MATH>` tag opens and closes correctly

### Next step

For a beginner walkthrough, see [Get Started with Calculations](broken://spaces/WKNnJmhJBQwhdk5WBFsi/pages/AWClXpysUyYPXaqZa7po).


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.sdocs.com/sdocs/advanced-template-logic/perform-calculations/math-tags-null-value-error-ending-position-out-of-bounds-22-stack-invalid-double.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
