# Divide by zero

**URL:** <https://forum.xojo.com/t/divide-by-zero/69484>\
**Category:** General\
**Created:** [April 6, 2022, 2:40pm UTC](https://forum.xojo.com/t/divide-by-zero/69484 "2022-04-06T14:40:40Z")\
**Posts on this page:** 16\
**Page:** 3

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [April 7, 2022, 1:18pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/41 "2022-04-07T13:18:09Z")

</div>

I think you’re saying that _any_ attempted conversation of INF or NAN to integer should raise an exception, and I agree. There is simply no non-arbitrary way to represent those values as integer, and any code that is doing that silently is just masking a bug.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [April 7, 2022, 1:23pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/42 "2022-04-07T13:23:55Z")

</div>

> [@Kem\_Tekinay](#):
>
> I think you’re saying that _any_ attempted conversation of INF or NAN to integer should raise an exception

Yes, unless you have some pragma to disable such check and know what you’re doing.

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [May 12, 2025, 12:02pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/43 "2025-05-12T12:02:27Z")

</div>

Until now, I firmly believed that Xojo would throw an exception when dividing by zero.  
I hope this will be changed, even if it might jeopardize some of the code that currently works.

---

<div class="post-metadata">

**Author:** ![Michael\_Hußmann](https://forum.xojo.com/user_avatar/forum.xojo.com/michael_hu%C3%9Fmann/32/438_2.png) [@Michael\_Hußmann](https://forum.xojo.com/u/Michael_Hu%C3%9Fmann)\
**Post date:** [May 12, 2025, 12:44pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/44 "2025-05-12T12:44:41Z")

</div>

I hope it stays as it is. The behavior is clearly documented and there is an obvious way to deal with it (using the IsNotANumber and IsInfinite methods), so there is really no problem that would require solving.

Criticize me if you wish but I even use a NaN value to indicate that some Double value is unknown: I divide by 0, store the result, and assign that whenever a value is unknown.

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [May 12, 2025, 1:14pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/45 "2025-05-12T13:14:41Z")

</div>

Xojo aims to be an easy-to-use alternative to other basic variants, especially for beginners. And I suspect there are quite a few beginners who use algorithms that can involve division by zero. If an undefined value is actually returned instead of an error, this erroneous output from the algorithm may be processed further. Unbeknownst to the code author, the results may be incorrect.

**Xojo’s current behavior isn’t wrong**. However, I personally would prefer an exception error at this point. 🙂

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [May 12, 2025, 3:06pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/46 "2025-05-12T15:06:08Z")

</div>

To keep the current behavior, but also enabling a proper check, a #Pragma should exist, as

```auto
#Pragma DivideByZeroError Default True // Sets current default
#Pragma DivideByZeroError Default False // System Default

#Pragma DivideByZeroError True // Change just the current method from here to its end
#Pragma DivideByZeroError False // Change just the current method from here to its end

```

So at App.Opening() you could put a **#Pragma DivideByZeroError Default True**

Putting a #Pragma Default in any place not except App.Opening() should lead to an error like “Defaults must be set as the first instructions. Try at App.Opening()”

It should affect just the current App being built, any pre-compiled content, as a plugin, should obey what was set there when it was compiled.

---

<div class="post-metadata">

**Author:** ![Michael\_Hußmann](https://forum.xojo.com/user_avatar/forum.xojo.com/michael_hu%C3%9Fmann/32/438_2.png) [@Michael\_Hußmann](https://forum.xojo.com/u/Michael_Hu%C3%9Fmann)\
**Post date:** [May 12, 2025, 3:47pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/47 "2025-05-12T15:47:48Z")

</div>

> [@Sascha\_S](#):
>
> If an undefined value is actually returned instead of an error, this erroneous output from the algorithm may be processed further. Unbeknownst to the code author, the results may be incorrect.

Thankfully any further calculations involving a NaN, INF, or -INF will result in another NaN, INF, or -INF value so the result will not be incorrect – just not a meaningful number.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [May 12, 2025, 4:06pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/48 "2025-05-12T16:06:53Z")

</div>

> [@Michael\_Hußmann](#):
>
> Thankfully any further calculations involving a NaN, INF, or -INF will result in another NaN, INF, or -INF value

Not true.

 ![image](https://forum.xojo.com/uploads/default/original/3X/7/c/7c797dbdc49a793ecce50a1ba49815178e5afdc8.png)

---

<div class="post-metadata">

**Author:** ![Michael\_Hußmann](https://forum.xojo.com/user_avatar/forum.xojo.com/michael_hu%C3%9Fmann/32/438_2.png) [@Michael\_Hußmann](https://forum.xojo.com/u/Michael_Hu%C3%9Fmann)\
**Post date:** [May 12, 2025, 4:28pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/49 "2025-05-12T16:28:03Z")

</div>

> [@Rick\_Araujo](#):
>
> Not true.

I had only checked Doubles operations where it appears to be true. Your examples involve conversions from Double to Integer and apparently NaN, INF, and -INF don’t get converted properly.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [May 12, 2025, 4:51pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/50 "2025-05-12T16:51:01Z")

</div>

> [@Rick\_Araujo](#):
>
> Floating point values can be numbers but also some “special values” that aren’t “numbers”. +Infinity, -Infinity and for all invalid operations a “Not a number” NaN. Floats as Doubles have special bits/constructs to carry such information.
> 
> For x = y / 0, x is not a number, it can be NaN, +Inf or -Inf depending on y. And values as NaN and Inf have no way to be stored in a Integer scalar. And that’s why such expressions rises exceptions in most current languages.

> [@Michael\_Hußmann](#):
>
> apparently NaN, INF, and -INF don’t get converted properly.

Because it’s impossible. That’s why exceptions arises when risk math operations occurs as DivByZero or losing meaning as Integer = NaN

To inform you of a “oops…” look at that!

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [May 13, 2025, 5:33am UTC](https://forum.xojo.com/t/divide-by-zero/69484/51 "2025-05-13T05:33:31Z")

</div>

Thank you @Rick_Araujo

Of course, one can try to prepare for such calculation errors, but even the most cautious users can miss situations where such errors lead to incorrect results. If machines (which may be working together with humans) are also being controlled, the consequences can be fatal.

But a DivisionByZero exception error would protect beginners and professionals alike from these errors. It probably wouldn’t make anyone’s life more difficult, but it would make life easier for many.

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [May 13, 2025, 10:05am UTC](https://forum.xojo.com/t/divide-by-zero/69484/52 "2025-05-13T10:05:09Z")

</div>

> [@Sascha\_S](#):
>
> a DivisionByZero exception error

agree to have this optional and care backwards compatibility.

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [May 13, 2025, 12:23pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/53 "2025-05-13T12:23:21Z")

</div>

That won’t happen. Nobody wants to only get certain exceptions if we enable them in the IDE settings or via a switch in the code. I, at least, would rather forgo implementation than have an exception thrown only optionally. 🙂

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [May 13, 2025, 2:19pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/54 "2025-05-13T14:19:48Z")

</div>

> [@Sascha\_S](#):
>
> Nobody wants to only get certain exceptions if we enable them in the IDE settings or via a switch in the code.

That’s a pretty sweeping statement. I think the Pragma suggestion is pretty sound - it strikes a balance between preserving existing behavior by default while providing an option for those who want it.

---

<div class="post-metadata">

**Author:** ![Michael\_Hußmann](https://forum.xojo.com/user_avatar/forum.xojo.com/michael_hu%C3%9Fmann/32/438_2.png) [@Michael\_Hußmann](https://forum.xojo.com/u/Michael_Hu%C3%9Fmann)\
**Post date:** [May 14, 2025, 11:19am UTC](https://forum.xojo.com/t/divide-by-zero/69484/55 "2025-05-14T11:19:36Z")

</div>

> [@Rick\_Araujo](#):
>
> Because it’s impossible.

The special values NaN, INF, and -INF only exist for floating point data types so they cannot be converted to integer values. One could designate some integer values as NaN, INF, and -INF, respectively, so a conversion would be possible in principle, but I agree that it’s probably not worth the trouble.

Having said that one could argue that a division by 0 with integer values _should_ cause an exception because the special values don’t exist for integers and one cannot test the results in retrospect with IsNotANumber or IsInfinite.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [May 14, 2025, 11:42am UTC](https://forum.xojo.com/t/divide-by-zero/69484/56 "2025-05-14T11:42:17Z")

</div>

> [@Michael\_Hußmann](#):
>
> a division by 0 with integer values _should_ cause an exception

That’s how all this thread started and evolved to other aspects about this kind of problem.

> [@Michael\_Hußmann](#):
>
> One could designate some integer values as NaN, INF, and -INF

One could, but not Xojo, and no computer language. In a computer language where integer is an object, those “values” would be set apart as flags, not in the value bits per se. So,no way, that’s… hmmm… 😱. It would increase the mess. NaN, Inf± in floats does not consume its mantissa bits, they use a special “flag”, the exponent with all bits set + a mantissa meaning, the float structure has this ability with no equivalent in scalar integers.  
No need to reinvent the wheel, exceptions handle that, just copy others.

[Previous page](https://forum.xojo.com/t/divide-by-zero/69484.md?page=2)
