# 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:** 20\
**Page:** 2

<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 6, 2022, 4:40pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/21 "2022-04-06T16:40:50Z")

</div>

> [@kevin\_g](#):
>
> I just think it should be a runtime switch and not forced upon users.

Compiler option. Project preference, and pragmas in code to switch on/off.  
If possible, I’m not against people opting for unsafe code if they know what they are doing.

---

<div class="post-metadata">

**Author:** ![Paul\_Lefebvre](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_lefebvre/32/3152_2.png) [@Paul\_Lefebvre](https://forum.xojo.com/u/Paul_Lefebvre)\
**Post date:** [April 6, 2022, 5:39pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/22 "2022-04-06T17:39:54Z")

</div>

I did create a case for this just last week:

68144 - Raise an exception when dividing by 0

---

<div class="post-metadata">

**Author:** ![Joost\_Rongen1](https://forum.xojo.com/user_avatar/forum.xojo.com/joost_rongen1/32/15052_2.png) [@Joost\_Rongen1](https://forum.xojo.com/u/Joost_Rongen1)\
**Post date:** [April 6, 2022, 6:00pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/23 "2022-04-06T18:00:50Z")

</div>

[quote=“Paul\_Lefebvre, post:22, topic:69484”]  
I did create a case for this just last week:

68144 - Raise an exception when dividing by 0  
[/quote

Thanks @Paul_Lefebvre , at least now it’s under consideration. And I agree with @Kem_Tekinay: “I’d argue that old code that is dividing by zero is already broken, and this change would merely expose the existing 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 6, 2022, 6:45pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/24 "2022-04-06T18:45:51Z")

</div>

> [@Paul\_Lefebvre](#):
>
> I did create a case for this just last week

👍🏻

I just want to add that in that case you said Swift does not trap div by zero. That’s what I get with Swift 5 and 5.1, both terminated:

 ![image](https://forum.xojo.com/uploads/default/original/2X/7/7dedcedd75eb9611b4d40fa2a1e3beb079326598.png)

 ![image](https://forum.xojo.com/uploads/default/original/2X/8/8ed983f7edcc0c583c77de4778fb292cf5157494.png)

 ![image](https://forum.xojo.com/uploads/default/original/2X/6/6af98ac79ea53542303fa99a6d4fb46bb6b9bc15.png)

 ![image](https://forum.xojo.com/uploads/default/original/2X/d/d3a8e52cd33a2bc57a4b7fd2067480a8a74e12ca.png)

☝🏻 The last case the 5.1 compiler detects div by zero at compile time and cancels

---

<div class="post-metadata">

**Author:** ![Paul\_Lefebvre](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_lefebvre/32/3152_2.png) [@Paul\_Lefebvre](https://forum.xojo.com/u/Paul_Lefebvre)\
**Post date:** [April 6, 2022, 7:05pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/25 "2022-04-06T19:05:41Z")

</div>

> [@Rick\_Araujo](#):
>
> I just want to add that in that case you said Swift does not trap div by zero. That’s what I get with Swift 5 and 5.1

Thanks for that. It’s possible I was looking at an older version of Swift.

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [April 7, 2022, 12:34am UTC](https://forum.xojo.com/t/divide-by-zero/69484/26 "2022-04-07T00:34:41Z")

</div>

> [@MarkusR](#):
>
> i think if a cake is not cut the cake stay original  
> so a=12/0  
> i would expect 12.

That’s not how division works. 0 is not the number of cuts, but the number of desired portions. How many portions of 0 cake can you get? Infinity.

---

<div class="post-metadata">

**Author:** ![Robert\_Weaver](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/919ad9/32.png) [@Robert\_Weaver](https://forum.xojo.com/u/Robert_Weaver)\
**Post date:** [April 7, 2022, 1:05am UTC](https://forum.xojo.com/t/divide-by-zero/69484/27 "2022-04-07T01:05:08Z")

</div>

In order not to break existing code it would need to be disabled by default and enabled like this:

```auto
#pragma EnableDivZeroException
```

Virtually all of my Xojo programming is very math intensive, and I’ve never felt the need for a divide by zero exception. In fact, the IEEE method of generating and propagating NaN’s is in my opinion a far better way of detecting math errors, but it applies only to floating point numbers.

---

<div class="post-metadata">

**Author:** ![J\_Andrew\_Lipscomb](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/a4c791/32.png) [@J\_Andrew\_Lipscomb](https://forum.xojo.com/u/J_Andrew_Lipscomb)\
**Post date:** [April 7, 2022, 3:43am UTC](https://forum.xojo.com/t/divide-by-zero/69484/28 "2022-04-07T03:43:32Z")

</div>

Exactly. A floating divide by zero should yield the IEEE codes (but we do need ways to check values to see if a variable in fact contains one of these). But an integer divide by zero can’t be signaled in the variable contents, so it needs to raise an exception. Also, coercing an infinite or NaN vale to an integer should be an exception.

---

<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:** [April 7, 2022, 6:18am UTC](https://forum.xojo.com/t/divide-by-zero/69484/29 "2022-04-07T06:18:33Z")

</div>

> [@Thom\_McGrath](#):
>
> That’s not how division works. 0 is not the number of cuts, but the number of desired portions. How many portions of 0 cake can you get? Infinity.

6=12/2  
50%=100% / 2  
or  
50%+50%=100% ← my 2 pieces of cake

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 7, 2022, 8:28am UTC](https://forum.xojo.com/t/divide-by-zero/69484/30 "2022-04-07T08:28:59Z")

</div>

My cake is 100, I will loop until all my parts of cake reach 100 :  
Loop until a = 100  
a = a + 0  
End loop

It will loop indefinitely

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [April 7, 2022, 10:04am UTC](https://forum.xojo.com/t/divide-by-zero/69484/31 "2022-04-07T10:04:02Z")

</div>

> [@MarkusR](#):
>
> 6=12/2  
> 50%=100% / 2  
> or  
> 50%+50%=100% ← my 2 pieces of cake

Yes, that is correct. What’s your point? I like the loop idea that @Thomas_ROBISSON made. To elaborate:

```xojo
Var Total As Integer = 12
Var Result As Integer
Const Divisor = 2
While Total > 0
  Total = Total - Divisor
  Result = Result + 1
Wend

```

Result will be 6. Now set to the divisor to 0:

```xojo
Var Total As Integer = 12
Var Result As Integer
Const Divisor = 0
While Total > 0
  Total = Total - Divisor
  Result = Result + 1
Wend

```

Total will remain 12 because nothing is ever removed. It is an infinite loop. Result is infinity, not 0.

One thing I really like about this example is that code after the loop won’t execute, further demonstrating the problems with dividing by zero. This is why most languages will throw an exception. There’s basically no other logical way to handle the scenario.

(Also, this loop won’t give correct answers when the divisor is something that doesn’t divide 12 evenly. That’s fine, this loop isn’t intended to be a replacement for division.)

---

<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:** [April 7, 2022, 10:47am UTC](https://forum.xojo.com/t/divide-by-zero/69484/32 "2022-04-07T10:47:05Z")

</div>

> [@Thom\_McGrath](#):
>
> Yes, that is correct. What’s your point?

from my point of view  
100%=100% / 0 ← no divide, its untouched, i do not need a error here  
50%=100% / 2  
25%=100% / 4

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [April 7, 2022, 11:03am UTC](https://forum.xojo.com/t/divide-by-zero/69484/33 "2022-04-07T11:03:34Z")

</div>

> [@MarkusR](#):
>
> from my point of view

Your point of view is irrelevant. Math is math and doesn’t give a damn about your point of view.

100 / 2 = 50  
100 / 1 = 100  
100 / 0 = Also 100? WTF?

You’re confusing dividing by 1 with dividing by 0.

---

<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, 11:04am UTC](https://forum.xojo.com/t/divide-by-zero/69484/34 "2022-04-07T11:04:00Z")

</div>

> [@MarkusR](#):
>
> 100%=100% / 0 ← no divide, its untouched, i do not need a error here

So you are telling us that a construct like this:

![image](https://forum.xojo.com/uploads/default/original/2X/c/cdd5dffa94141385fa34b24eb1e27c21d96a596c.png)

is not a fraction?

No, it can NOT be 0 as a result of such calculation (nor a large positive or negative integer as Xojo does when doing int math). It can be a zero IF you desire trapping math errors and ASSUMING zero because you want and know what you are doing.

Markus, this is standard. No need to reinvent behaviors or distort how math works.

> **[x = 100/0 - Wolfram|Alpha](https://www.wolframalpha.com/input?i=x%2B%3D%2B100%2F0)**
>
> Wolfram|Alpha brings expert-level knowledge and capabilities to the broadest possible range of people—spanning all professions and education levels.

---

<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:** [April 7, 2022, 11:07am UTC](https://forum.xojo.com/t/divide-by-zero/69484/35 "2022-04-07T11:07:55Z")

</div>

> [@Thom\_McGrath](#):
>
> Math is math

sure  
in math you can divide a cake by 0.5 and you will have 2 🤯

---

<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, 11:09am UTC](https://forum.xojo.com/t/divide-by-zero/69484/36 "2022-04-07T11:09:54Z")

</div>

Markus, division is the inverse of the multiplication. Can you get what am I saying?

You think that is unnatural 1 / (1/2) = 2, but probably don’t care when 2 \* 1/2 = 1

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [April 7, 2022, 11:23am UTC](https://forum.xojo.com/t/divide-by-zero/69484/37 "2022-04-07T11:23:27Z")

</div>

> [@MarkusR](#):
>
> sure  
> in math you can divide a cake by 0.5 and you will have 2

You have to be trolling me at this point. So are you suggesting that multiplication doesn’t exist, simply because I can’t multiply a cake by 4 to have 4 cakes? You also can’t divide a cake by 6,000,000,000 because then you just have dust. Can you still call it a cake? OMG, math is a lie isn’t it? [What if it’s all just cake?](https://www.netflix.com/title/81333845)

---

<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, 11:57am UTC](https://forum.xojo.com/t/divide-by-zero/69484/38 "2022-04-07T11:57:43Z")

</div>

Let’s tone it down and get back on topic.

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 7, 2022, 12:16pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/39 "2022-04-07T12:16:29Z")

</div>

No Kem, we will loop until Markus agree with us ! 🙂 😃 .

I’m not on my computer then I can’t test. But if I well understood under Xojo a = 3 / 0 does not generate an error ? If a is an integer or a double it should as it is supposed to contain a number, not a string or a boolean or a folderitem. The result of a number divided by 0 is not a number then couldn’t be stored in a variable supposed to contain 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:** [April 7, 2022, 12:59pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/40 "2022-04-07T12:59:00Z")

</div>

> [@Thomas\_ROBISSON](#):
>
> But if I well understood under Xojo a = 3 / 0 does not generate an error ? If a is an integer or a double it should as it is supposed to contain a number, not a string or a boolean or a folderitem.

Variables supports values. 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. If you move its bits to ints, it will result in any valid random number causing confusion and being used for mistaken calculations.

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

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