# Check Division by Zero in JavascriptEngine

**URL:** <https://forum.juce.com/t/check-division-by-zero-in-javascriptengine/13224>\
**Category:** General JUCE discussion\
**Created:** [July 22, 2014, 5:33pm UTC](https://forum.juce.com/t/check-division-by-zero-in-javascriptengine/13224 "2014-07-22T17:33:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [July 22, 2014, 5:33pm UTC](https://forum.juce.com/t/check-division-by-zero-in-javascriptengine/13224/1 "2014-07-22T17:33:48Z")

</div>

Hi Jules,&nbsp;

&nbsp;

I am playing around with the marvelous JavascriptEngine and discovered that it really hates division by zero.&nbsp;If you type '1 / 0' into the editor of the Demo, it instantly crashes, which makes stupid script developers a bit too powerful for my taste :)

It would be no problem to catch the exception everytime you run a script, but why didn't you implement the check right at the spot?

```
​&nbsp;struct DivideOp &nbsp;: public BinaryOperator &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; DivideOp (const CodeLocation& l, ExpPtr& a, ExpPtr& b) noexcept : BinaryOperator (l, a, b, TokenTypes::divide) {} &nbsp; &nbsp; &nbsp; &nbsp; var getWithDoubles (double a, double b) const override &nbsp;{ return a / b; } &nbsp; &nbsp; &nbsp; &nbsp; var getWithInts (int64 a, int64 b) const override &nbsp; &nbsp; &nbsp; { return a / b; } &nbsp; &nbsp; };
```

something like

```
var getWithInts (int64 a, int64 b) const override       { return b == 0 ? var::undefined() : a / b;  }
var getWithDoubles (double a, double b) const override  { return b == 0.0 ? var::undefined() : a / b; }
```

would prevent the division by zero problem while giving a nice feedback if you do something stupid and doesn't introduce too much overhead.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 22, 2014, 5:37pm UTC](https://forum.juce.com/t/check-division-by-zero-in-javascriptengine/13224/2 "2014-07-22T17:37:21Z")

</div>

Excellent suggestion! Is 'undefined' the correct value for a divide by zero in JS?

---

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [July 22, 2014, 5:50pm UTC](https://forum.juce.com/t/check-division-by-zero-in-javascriptengine/13224/3 "2014-07-22T17:50:35Z")

</div>

Well,&nbsp;there is something called&nbsp;'Infinity':

http://www.w3schools.com/jsref/jsref\_infinity.asp

but I don't know how important this is. I think most of the time you made something wrong if you divide by zero, so I guess getting var::undefined should be enough of a hint...

If you check it in, don't forget the modulo operator!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 22, 2014, 6:42pm UTC](https://forum.juce.com/t/check-division-by-zero-in-javascriptengine/13224/4 "2014-07-22T18:42:46Z")

</div>

Hmm, looks like the correct thing to do would actually be to return a double which is set to infinity, as that's what other JS interpreters do.
