<p>The best way that I know is quite involved because bitcoin-core standardness rules around Minimal representation of script ints. If you really need to do this, I can add detail in an updated answer.</p>
<p><strong>Execution vs Satisfaction:</strong></p>
<p>But in almost all cases, you should never really need to check such a thing. Recall that Bitcoin Script satisfaction requires Script executes successfully. At the end of the day, we are not interested in what is execution result, we are interested in whether the witness is valid or not. And the user redeeming the coins knows whether the pushed number is ScriptInt or not, they are let the interpreter know via additional boolean input.</p>
<pre><code>Previous Code: where stack top is element `e`
OP_ISTOPNUMERIC # hypothetical that pushes 1 or 0 onto the stack top
OP_IF 
    &lt;process_num&gt;
OP_ELSE
    &lt;something_else&gt;
OP_ENDIF
</code></pre>
<p>can be read written equivalently with two user input elements <code>[e, e_is_num]</code> where <code>e_is_num</code> is at stack top denoting whether <code>e</code> is numeric or not.</p>
<pre><code>New code: with stack as `[e, e_is_num]`
OP_IF 
    &lt;process_num&gt;
OP_ELSE
    &lt;something_else&gt;
OP_ENDIF
</code></pre>

CC BY-SA 4.0
2021-01-27T03:08:57.503Z
2021-01-27T03:08:57.503Z
sanket1729
<p>I'm looking for the clearest way to check whether the top element of the stack is numeric or not.</p>
<p>Thank you in advance</p>

<script><opcodes>
Script: How to check if the top item on the main stack is numeric or not?
FraMog
# Script: How to check if the top item on the main stack is numeric or not?

(Asked by: username_0 on 2021-01-26)

I'm looking for the clearest way to check whether the top element of the stack is numeric or not.

Thank you in advance

**Tags:** script, opcodes


---

## 1 answers

### Answer by username_1 on 2021-01-27. Score: 1

The best way that I know is quite involved because bitcoin-core standardness rules around Minimal representation of script ints. If you really need to do this, I can add detail in an updated answer.

**Execution vs Satisfaction:**

But in almost all cases, you should never really need to check such a thing. Recall that Bitcoin Script satisfaction requires Script executes successfully. At the end of the day, we are not interested in what is execution result, we are interested in whether the witness is valid or not. And the user redeeming the coins knows whether the pushed number is ScriptInt or not, they are let the interpreter know via additional boolean input.

```
Previous Code: where stack top is element `e`
OP_ISTOPNUMERIC # hypothetical that pushes 1 or 0 onto the stack top
OP_IF 
    <process_num>
OP_ELSE
    <something_else>
OP_ENDIF

```

can be read written equivalently with two user input elements `[e, e_is_num]` where `e_is_num` is at stack top denoting whether `e` is numeric or not.

```
New code: with stack as `[e, e_is_num]`
OP_IF 
    <process_num>
OP_ELSE
    <something_else>
OP_ENDIF

```


---

