# 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:** 1

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

</div>

Since Xojo is restyling the coding syntaxes to more human and readable code, at least that was the intention, I should also expect some more human operation such as a runtime error when a divide by zero occurs.  
I remember a there was a scientific approach to have it behaving as it does, but despite that i believe Xojo should follow Microsoft approach by raising a runtime error if the coder did not check for a divide by zero.  
Anybody who has a good reason to keep it as it works now ?

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [April 6, 2022, 2:45pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/2 "2022-04-06T14:45:09Z")

</div>

This may help:

> [@Xojo divides by ZERO?](https://forum.xojo.com/t/xojo-divides-by-zero/69235/5):
>
> Xojo is not a language that has ever raised an exception for divide by 0. Docs say: If you divide by zero, the result is positive or negative infinity, except if the numerator is also zero. In that case, the result is NaN. For that reason, you should always check to see whether the denominator is zero before using / .

---

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

</div>

i think if a cake is not cut the cake stay original  
so a=12/0  
i would expect 12.

createing a runtime error for everything is not a good approach.  
that was the reason i wrapped every ms vb6 method into own one.  
i used methods that return a boolean if they done the task, so you can control the app flow.  
using try catch for all methods would make your code look very ugly.

at least xojo should raise a error in debug mode in ide for the developer.  
at runtime in release mode i think the math operation should be ignored as mentioned above.

be careful, xojo could create different values for 32/64bit.

to be save you could calculate the divide by a method.

---

<div class="post-metadata">

**Author:** ![kevin\_g](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kevin\_g](https://forum.xojo.com/u/kevin_g)\
**Post date:** [April 6, 2022, 3:34pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/4 "2022-04-06T15:34:12Z")

</div>

If there was some kind of runtime property that allowed the current functionality to be maintained then I guess it would be no problem allowing exceptions to be raised if the user wanted them.

If Xojo just changed functionality then it would be another backwards compatibility killer.

---

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

</div>

I don’t see how. It would just expose code that already isn’t working properly, no?

---

<div class="post-metadata">

**Author:** ![kevin\_g](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kevin\_g](https://forum.xojo.com/u/kevin_g)\
**Post date:** [April 6, 2022, 3:45pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/6 "2022-04-06T15:45:45Z")

</div>

It would expose code which isn’t handling it but would break code which is handling it.

---

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

</div>

If code is handling it, it won’t divide by zero, so it won’t see any exception. Or do you mean something else?

---

<div class="post-metadata">

**Author:** ![kevin\_g](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kevin\_g](https://forum.xojo.com/u/kevin_g)\
**Post date:** [April 6, 2022, 3:54pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/8 "2022-04-06T15:54:00Z")

</div>

Yes, checking the result afterwards.

---

<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:09pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/9 "2022-04-06T16:09:51Z")

</div>

> [@MarkusR](#):
>
> a=12/0  
> i would expect 12.

n/0 is not possible, so the result is undefined, it’s not a number. Undefined can’t be expressed as an integer number and that’s why typed languages (besides Xojo) raises an exception when someone tries it. Float types can carry some special values, and for undefined values it is NaN (not -Inf or +Inf). Trying to set a NaN to an Integer should rise an exception. The reason that ignoring the exception and trying to attribute the bits of the value in the floating value of a NaN results in a large number is that NaN has an internal value with lots of bits set as 1, usually the signal, all the exponent bits, and part of mantissa, usually high bits. To be true, many people just set the higher 13 bits of 64 bit float (Double) when they need to express a NaN (NaN is a kind of value, not a constant, there’s not just ONE NaN value, there are millions of possible NaN combinations if you play with the lower bits.

---

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

</div>

let me explain different.  
xojo could made optional settings how xojo behave, to make the developer happy.  
see VB6 =Val(“Hello”) result in 0  
see .net =Val(“Hello”) result in error (very unhandy) without error handling the app runs awhile and then it ■■■■■■ up. (at wrong input)

---

<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:16pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/11 "2022-04-06T16:16:38Z")

</div>

How? There’s no safe form without an Exception during the math expression failing. The result silently transferred to an integer as possible real value (and probably kind of random) can’t be compared and assured being result of an invalid math operation. It just can make silent mess to those who don’t take proper previous measures (as usually do what Xojo calls “Citizen devs”).

---

<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:19pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/12 "2022-04-06T16:19:43Z")

</div>

> [@MarkusR](#):
>
> .net =Val(“Hello”) result in error

It will break at this point, correct? As it should be.

.Net is a modern language where Exception handling is part of. So, if you can introduce errors in the expression, you can catch them and take actions. Let’s imitate VB6:

```auto
Try
   x = Val(“Hello”)
Catch
   x = 0
End Try

```

---

<div class="post-metadata">

**Author:** ![kevin\_g](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kevin\_g](https://forum.xojo.com/u/kevin_g)\
**Post date:** [April 6, 2022, 4:26pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/13 "2022-04-06T16:26:04Z")

</div>

```auto
  Dim v As Double
  Dim v2 As Double
  
  v = 5 / 0 * 5
  v2 = Log(-1)
  
  If Str(v) = "inf" Then
    Break
  End If
  
  If IsInfMBS(v) = True Then
    Break
  End If
  
  If Str(v2) = "nan" Then
    Break
  End If
  
  If IsNANMBS(v2) = True Then
    Break
  End If
  
  Break

```

---

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

</div>

> “It will break at this point, correct? As it should be.”

no it should be bullet proved. if input is “” then it should return 0.  
the developer know how to use the method as it is.  
you can not make error handling for each piece of method you need to use.  
have you considered what happens if you need this val 1000 times in your source code?

---

<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:26pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/15 "2022-04-06T16:26:42Z")

</div>

What is this?

---

<div class="post-metadata">

**Author:** ![kevin\_g](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kevin\_g](https://forum.xojo.com/u/kevin_g)\
**Post date:** [April 6, 2022, 4:27pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/16 "2022-04-06T16:27:31Z")

</div>

You asked how you could detect it and I provided some very basic examples.

---

<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:28pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/17 "2022-04-06T16:28:09Z")

</div>

Not standard. Subject to fails. And worse, depends on plugins.

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [April 6, 2022, 4:29pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/18 "2022-04-06T16:29:36Z")

</div>

~~Couldn’t API 2 raise an exception like modern programmers expect and API 1 could be left alone. Just like everything else?~~

Nope that won’t work, we’d need a switch.

---

<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:34pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/19 "2022-04-06T16:34:04Z")

</div>

> [@MarkusR](#):
>
> no it should be bullet pro ~~ved~~ of

Breaking on unexpected errors as it does makes it. The VB6 (now defunct) behavior was fixed, not replicated.

---

<div class="post-metadata">

**Author:** ![kevin\_g](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kevin\_g](https://forum.xojo.com/u/kevin_g)\
**Post date:** [April 6, 2022, 4:37pm UTC](https://forum.xojo.com/t/divide-by-zero/69484/20 "2022-04-06T16:37:18Z")

</div>

The code shows how to do it without plugins.

FYI. I’m not against having it as a feature. I just think it should be a runtime switch and not forced upon users.

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