# Arithmetic overflow

**URL:** <https://forum.dfinity.org/t/arithmetic-overflow/7955>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [October 15, 2021, 11:16pm UTC](https://forum.dfinity.org/t/arithmetic-overflow/7955 "2021-10-15T23:16:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![infu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/infu/32/11321_2.png) [@infu](https://forum.dfinity.org/u/infu)\
**Post date:** [October 15, 2021, 11:16pm UTC](https://forum.dfinity.org/t/arithmetic-overflow/7955/1 "2021-10-15T23:16:35Z")

</div>

This script, which divides two numbers ends up giving error “execution error, arithmetic overflow”. Is that normal ? Can’t find anything in documentation about that.

```auto
var c:Nat = 0;
while(c < 100000) {
Debug.print("test...." # Nat.toText(c) # " " # Nat32.toText(136/32));
c := c + 1;
}

```

Right before the end it prints  
test…65471 160  
test…65472 160  
test…65473 160  
test…65474 160

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [October 17, 2021, 12:42pm UTC](https://forum.dfinity.org/t/arithmetic-overflow/7955/2 "2021-10-17T12:42:38Z")

</div>

That indeed looks like a bug. Is this in compiled code or the interpreter?

I’m on vacation but opened an issue.

> <https://github.com/dfinity/motoko/issues/2844>
>
> https://forum.dfinity.org/t/arithmetic-overflow/7955

---

<div class="post-metadata">

**Author:** ![infu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/infu/32/11321_2.png) [@infu](https://forum.dfinity.org/u/infu)\
**Post date:** [October 17, 2021, 6:04pm UTC](https://forum.dfinity.org/t/arithmetic-overflow/7955/3 "2021-10-17T18:04:22Z")

</div>

The command I use is

```auto
 moc -r `vessel sources` something.mo 

```

I have isolated it to this, which also gives the error.

```auto
import Nat32 "mo:base/Nat32";

loop {
ignore Nat32.toText(136/32);
};

```

The bug occurs with Int64,Nat64,Nat32 and similar…, but doesn’t occur with Int.toText and Nat.toText

Sometimes I see it pointing:  
“/src/Int.mo:31.17-31.27: execution error, arithmetic overflow”

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [October 18, 2021, 10:18am UTC](https://forum.dfinity.org/t/arithmetic-overflow/7955/4 "2021-10-18T10:18:56Z")

</div>

The `-r` option instructs `moc` to run your snippet in the interpreter. I have seen cases where loops were restricted to a certain number of iterations (maybe due to the interpreter not being tail-recursive in that case). My hunch is that the tight stack situation might crosstalk into the arithmetic evaluator.

Please note that the interpreter was only intended to try out ideas by running toy snippets/programs. It is not up to the same standards as the compiler, and has a number of shortcomings.
