# Divison by zero

**URL:** <https://forum.xojo.com/t/divison-by-zero/28310>\
**Category:** macOS\
**Created:** [November 26, 2015, 10:52pm UTC](https://forum.xojo.com/t/divison-by-zero/28310 "2015-11-26T22:52:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torben\_Vikow](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/d2c977/32.png) [@Torben\_Vikow](https://forum.xojo.com/u/Torben_Vikow)\
**Post date:** [November 26, 2015, 10:52pm UTC](https://forum.xojo.com/t/divison-by-zero/28310/1 "2015-11-26T22:52:32Z")

</div>

How to you handle/detect division by zero in your apps for the different data types?

This code has the following result

```auto
  dim val1 as Currency
  val1 = 0.0 / 0.0 
  TextArea1.Text = val1.ToText + EndOfLine
  
  val1 = 1.0 / 0.0
  TextArea1.Text = TextArea1.Text + val1.ToText + EndOfLine
  
  
  dim val2 as double
  val2 = 0 / 0
  TextArea1.Text = TextArea1.Text + val2.ToText + EndOfLine
  
  val2 = 1 / 0
  TextArea1.Text = TextArea1.Text + val2.ToText + EndOfLine
```

Result in TextArea1:

```auto
-0.5808
-0.5808
nan
inf
```

For me the values for the currency data type seems to be confusing because they also can be real results.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [November 26, 2015, 11:34pm UTC](https://forum.xojo.com/t/divison-by-zero/28310/2 "2015-11-26T23:34:50Z")

</div>

IsNanMBS() function helps me.

as well as IsInfMBS and IsFiniteMBS.

Or check yourself the arguments before dividing.

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [November 26, 2015, 11:45pm UTC](https://forum.xojo.com/t/divison-by-zero/28310/3 "2015-11-26T23:45:16Z")

</div>

> [@232267:@Torben Vikow](#):
>
> How to you handle/detect division by zero in your apps for the different data types?

Just dont perform the division

```auto
  dim val1 as Currency
  if divisor <> 0.0 then
    val1 = 0.0 / divisor
    TextArea1.Text = val1.ToText + EndOfLine
 
    val1 = 1.0 / divisor
    TextArea1.Text = TextArea1.Text + val1.ToText + EndOfLine
  end if
  
  dim val2 as double
  if divisor <> 0.0 then
     val2 = 0 / divisor
    TextArea1.Text = TextArea1.Text + val2.ToText + EndOfLine
  
    val2 = 1 / divisor
    TextArea1.Text = TextArea1.Text + val2.ToText + EndOfLine
  end if
```

Why ? Because Inf and Nan have no representation in an integer - only in doubles  
So this is the only reliable way to avoid issues in ALL numeric types  
And comparison to 0 or 0.0 is safe in all of them as well

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [November 26, 2015, 11:48pm UTC](https://forum.xojo.com/t/divison-by-zero/28310/4 "2015-11-26T23:48:57Z")

</div>

Oh and because INF and Nan in doubles may have many different representations quite legally  
See [https://en.wikipedia.org/wiki/NaN](https://en.wikipedia.org/wiki/NaN) and [https://en.wikipedia.org/wiki/IEEE\_floating\_point](https://en.wikipedia.org/wiki/IEEE_floating_point)  
IEEE 754 NaNs are represented with the exponent field filled with ones (like infinity values), and some non-zero number in the significand (to make them distinct from infinity values); this representation allows the definition of multiple distinct NaN values, depending on which bits are set in the significand, but also on the value of the leading sign bit (not all applications are required to provide distinct semantics for those distinct NaN values).

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:04pm UTC](https://forum.xojo.com/t/divison-by-zero/28310/5 "2020-10-29T18:04:38Z")

</div>


